Data Structures
C Programming (re-view) Pengampu : TATI ERLINA, M.I.T.
McGraw-Hill Technology Education
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved.
C Programming Review • What is an algorithm? • What is a variable? How do you declare a variable? • What is a function? • What is an if statement? When would you use one? • What is a loop? What are the different types of loops? When would you use each type? • What is a pointer? • What is an array? • What is a structure?
Pengenalan Bahasa C • dibuat pada tahun 1978 untuk Sistem Operasi Unix oleh Bell Labs (Ken Thompson dan Dennis M. Ritchie). • Buku The C Programming Language
• memperbolehkan pengaksesan memori secara manual (dengan POINTER).
Some programmer jargon 1. Source code: •
kode program yang ditulis programmer.
2. Compile (build): •
pengubahan source code ke dalam object code (bisa bahasa mesin / assembly)
3. Executable: •
program dalam bahasa mesin yang siap dieksekusi.
4. Library: •
fungsi-fungsi yang digunakan pada pembuatan program.
5. Preprocessor Directive 1. 2.
Dimulai dengan tanda # Header file: file yang berekstensi .h yang disertakan pada pembuatan program.
Identifier • adalah pengingat tempat penyimpanan data di dalam memori komputer. a. Variabel : bisa diubah b. Konstanta : bersifat tetap
Structure of C
• Blok utamanya adalah: • Preprocessor Directive • Function Definitions • Data Structures • Code programs • Function Body
#include <….> #define …. int coba(); void main() { int a; printf(“Hello, world!\n”); a = coba(); } int coba(){ ….. }
More about Hello World Preprocessor #include <stdio.h>
Comments are good
/* My first C program which prints Hello World */
main() means “start here” int main (int argc, char *argv[]) { printf ("Hello World!\n"); return 0; }
Brackets define code blocks
Library command
Return 0 from main means our program finished without errors
Keywords of C • 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
• Evil keywords which we avoid (1) – goto
Variable • tipe data setiap variabel pada C harus didefinisikan. • Setiap varibel punya tipe data dan namanya. • Variabel adalah unik, tidak boleh berupa keyword, dimulai dengan huruf atau underline, maks 32 karakter int a,b; double d; /* kurang jelas*/
int start_time; int no_students; double course_mark; /* lebih baik*/
Pendeklarasian Variabel & Konstanta
The char type • char disimpan dalam kode ascii (integer) • Print 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); return 0;
}
Escape Characters
A short note about ++ • ++i naikkan nilai kemudian gunakan • i++ gunakan kemudian naikkan nilai int i= 6; printf ("%d\n",i++);
/* Prints 6 sets i to 7 */
Perbedaan ini penting int i= 6; printf ("%d\n",++i);
/* prints 7 and sets i to 7 */
All of the above also applies to --.
Formatting Command Summary Format Command
Data type
Description
%d
Int
Decimal number
%x
Int
Hexadecimal number
%b
Int
Low byte as binary number
%c
Int
Low byte as ASCII character
%f
float
Floating point number
%s
char array
Char array (string)
Control Structure 1 IF / IF … ELSE
SWITCH
if ( true ) { DoFirstThing(); DoSecondThing(); };
switch ( key ) { case ‘a’: case ‘A’: DoFirstThing(); DoSecondThing(); break; case ‘b’: DoSomething(); break; default: break; };
if ( true ) DoSomething(); else DoSomethingElse();
Control Structure 2 FOR
WHILE
int i, j; for (i=0; i<5; i++) for (j=5; j>0; j--) { // i counts up // j counts down printf(“%i %j\n”, i, j); };
int i = 0; int StayInLoop = 1; while ( StayInLoop ) { i+=2; // Make sure you have // exit condition! if ( i > 200 ) StayInLoop = 0; };
The “++” / ”--” is shortcut used to increment / decrement value of int variables
“+=“ increments by n
What is function? • function adalah sub-unit dari sebuah program yang melaksanakan tugas tertentu. • Salah satu fungsi yang sudah digunakan – printf. • Functions menerima arguments (variables) dan dapat pula mengembalikan argument.
Jenis-jenis function 1. Void 2. Non-void
Contoh function #include <stdio.h> int maximum (int, int);
/* Prototype – see later in lecture */
int main(int argc, char*argv[]) Prototype the function { int i= 4; int j= 5; Panggil function int k; k= maximum (i,j); /* Call maximum function */ printf ("%d is the largest from %d and %d\n",k,i,j); printf ("%d is the largest from %d and %d\n",maximum(3,5), 3, 5); return 0; } function header int maximum (int a, int b) /* Return the largest integer */ function body { if (a > b) return a; /* Return means "I am the result of the function"*/ return b; /* exit the function with this result */ }
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 instruksiinstruksi yang ada di sana.
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