Refresh of
C languages
Refresh C Language
• Bahasa C dibuat pada tahun 1978 untuk Sistem Operasi Unix oleh Bell Labs (Ken Thompson dan Dennis M. Ritchie).
– Buku The C Programming Language
• Bahasa C merupakan salah satu bahasa pemrograman yang
paling sering dipakai oleh pemrogram di seluruh dunia, terutama karena bahasa C memperbolehkan pengakses memori secara manual. (dengan POINTER)
• Bahasa C menjadi dasar bahasa C++.
• Bahasa C seringkali dipakai untuk membuat bahasa-bahasa
pemrograman yang lain.
Identifier dan Tipe Data
• Identifier adalah pengingat tempat penyimpanan data di
dalam memori komputer.
– Variabel : bisa diubah
Some programmer jargon
•
Beberapa istilah:
– Source code: kode program yang ditulis programmer.
– Compile (build): pengubahan source code ke dalam object
code (bisa bahasa mesin / assembly)
– Executable: program dalam bahasa mesin yang siap
dieksekusi.
– Language: bahasa pemrograman.
– Library: fungsi-fungsi yang digunakan pada pembuatan
program.
– Preprocessor Directive
• Dimulai dengan tanda #
Structure of C Language
• Consists mainly of:
• Preprocessor Directive • Function Definitions • Data Structures
• Code programs • Function Body
#include <….>
#include <stdio.h>
/* My first C program which prints Hello World */
int main (int argc, char *argv[]) { main() means “start here”
Comments are good
Return 0 from main means our program finished without errors
Brackets
Keyword
•
Flow control (6) –
if, else, return,
switch, case, default
•
Loops (5) –
for, do, while, break,
continue
•
Common
types
(5) –
int, float,
double, char, void
•
Structures
(2) –
struct, typedef
•
Sizing things (1) –
sizeof
•
Rare but still useful
types
(7) –
extern,
signed, unsigned, long, short,
static, const
Variable
•
Kita harus mendeklarasikan tipe data setiap
variabel pada C.
•
Setiap varibel punya tipe data dan namanya.
•
Variabel adalah unik, tidak boleh berupa
keyword, dimulai dengan huruf atau
underline, maks 32 karakter
•
Deklarasi tipe data variable
<tipe data> <nama_variabel>;
•
Deklarasi konstanta
#define <nama_konstanta> <nilai>
int umur;
Escape Character
Karakter Escape Arti
\a Bunyi Bel (speaker komputer)
\b Mundur satu spasi (backspace)
\f Ganti Halaman (form feed)
\n Ganti Baris Baru (new line)
\r Ke kolom pertama baris yang sama (carriage return)
\t Tabulasi Horizontal
\v Tabulasi Vertical
\0 Nilai Kosong (null)
\’ Karakter Petik Tunggal
\” Karakter Petik Ganda
The
Char
Type
•
char
disimpan dalam kode ascii (integer)
•
char
dengan
%c
•
char
menggunakan
single quote
int main() {
char a, b;
a= 'x'; /* Set a to the character x */ printf ("a is %c\n",a);
b= '\n'; /* This really is one character*/ printf ("b is %c\n",b);
A short note about
++
•
++i means increment i then use it
•
i++ means use i then increment it
VMD / 13
int i= 6;
printf ("%d\n",i++); /* Prints 6 sets i to 7 */
int i= 6;
printf ("%d\n",++i); /* prints 7 and sets i to 7 */
Note this important difference
Casting
•
Memaksa suatu tipe data
•
Tipe data yang serupa
Format Command
Format Data Type Description
%d Int Decimal Number
%x Int Hexa Number
%b Int Low byte as binary number %c Int Low byte as ASCII character %f Float Floating point number
Control Structure
(lanjutan)used to increment / decrement value of int variables
WHILE
int i = 0;
int StayInLoop = 1; while ( StayInLoop ) {
i+=2;
// Make sure you have // exit condition!
if ( i > 200 )
StayInLoop = 0;
};
What is a function?
•
The
function
is one of the most basic things to
understand in C programming.
•
A
function
is a sub-unit of a program which performs
a specific task.
•
We have already (without knowing it) seen one
function from the C library –
printf.
•
We need to learn to write our own functions.
•
Functions take
arguments
(variables) and may return
an
argument.
Type of function
•
Void
: tidak mengembalikan nilai
An Example of Function
#include <stdio.h>
int maximum (int, int); /* Prototype – see later in lecture */
int main(int argc, char*argv[]) {
/* Return the largest integer */ {
if (a > b)
return a; /* Return means "I am the result of the function"*/ return b; /* exit the function with this result */
}
Prototype the function
Call the function
The
main
Function
•
function main() dibutuhkan agar program C
dapat dieksekusi!
•
Tanpa function main, program C dapat
dicompile tapi tidak dapat dieksekusi (harus
dengan flag parameter –c, jika di UNIX)
•
Pada saat program C dijalankan, maka
compiler C pertama kali akan mencari function
main() dan melaksanakan instruksi-instruksi
int main()
• Berarti di dalam function main tersebut harus terdapat keyword return di bagian akhir fungsi dan mengembalikan nilai bertipe data int,
• Mengapa hasil return harus bertipe int juga? karena tipe data
yang mendahului fungsi main() diatas dideklarasikan int
• Tujuan nilai kembalian berupa integer adalah untuk
mengetahui status eksekusi program.
– jika “terminated successfully” (EXIT_SUCCESS) maka, akan dikembalikan status 0,
– sedangkan jika “terminated unsuccessfully” (EXIT_FAILURE) akan dikembalikan nilai status tidak 0, biasanya bernilai 1
What is scope variable?
•
The
scope
of a variable is where it can be used in a
program
•
Normally variables are
local
in
scope -
this means
they can only be used in the function where they are
declared (main is a function)
•
We can also declare
global
variables.
•
If we declare a variable outside a function it can be
Other techniques for debugging
•
Check missing brackets and commas.
•
Check that you have a semicolon at the end of every
line which needs one.
•
Put in some
printf
– if you know what your program is DOING you will know
what it is DOING WRONG.