\center \large Δομές Δεδομένων και Αλγόριθμοι\ Πρόχειρες σημειώσεις του Α. Καμπουρέλη, ΤΕΙ Ηρακλείου, τμήμα Ε.Π.Π. \par Program, RAM model and functions \par Building program \list 1. Source program (text) in C or C++ (.c or .cpp files) 2. Pre-processor: #include = insert library header(s) (.h files, like ) #define = resolve constants and macros (like Find and Replace) 3. C compiler - compile to machine code (maybe virtual, like .NET) 4. Linker - insert compiled libraries (.lib files) 5. Create executable (.exe) file \par Basics 1 Bit = 0 or 1 1 Byte = 8 Bits RAM model as a huge array[address] of bytes. --EXPAND this!!! Picture!!! 1 KB (KiloByte) = 1024 Byte 1 MB (MegaByte) = 1024 KB = 1,048,576 Bytes 1 GB (GigaByte) = 1024 MB = 1,073,741,824 Bytes 1 TB (TeraByte) = 1024 GB = 1,099,511,627,776 Bytes Variables in C External (global) variables, are defined out of any function (including the main function), usually, at the beginning of our program, after all #include and #define lines. External variables are always zeroed (cleared) at the start of running. Internal (local) variables, are variables declared inside any function (including main). Internal variables are NOT cleared! May contain garbage! You MUST initialize them! Try to avoid using external variables! -- expand this topic! static variables, etc.... *Address and size of variable &v = address of variable v (pointer to v). In fact, &v is the address of the FIRST byte of space reserved for v. sizeof(v) = size (in bytes) of space reserved for v. It depends only on type of v, so, one can also use sizeof(int), sizeof(float) etc. Note that sizeof() is platform dependent! For example: sizeof(int) = 2 in 16 bit DOS, but sizeof(int) = 4 in 32 bit Windows. // example, address and sizeof of variables #include main(){ int n; float f; double d; char c; char p[100]; // πίνακας 100 χαρακτήρων printf("%d %d \n ", &n, sizeof(n)); printf("%d %d \n ", &f, sizeof(f)); printf("%d %d \n ", &d, sizeof(d)); printf("%d %d \n ", &c, sizeof(c)); printf("%d %d \n ", &p, sizeof(p)); c = 'A'; p[100] = 'B'; // out of range error printf( "c = %c \n", c ); // what, if c represents the blood group of your friend? } *Functions in C type abc(parameters); // declaration of abc (δήλωση) type abc(parameters){ // definition of abc (ορισμός) ... code ... } abc(actual parameters); // call of abc (κλήση) example: float mysin(float x); // declaration of mysin main(){ float x,y; y = mysin(55); // call of mysin x = 66; y = mysin(x); // another call of mysin } float mysin(float x){ return sin(5*x+10); } // definition of mysin *Note Declarations are not necessary, but sometimes are very usefull for understanding our program. Without declaration, any function must be defined BEFORE any call to it. example: float mysin(float x){ return sin(5*x+10); } // definition main(){ float x,y; y = mysin(55); // call } Example of void function: void BadNews(){ printf("You have a virus! \n"); } main(){ ... BadNews(); // call it, note the parenthesis! } Remark: C function may return: int, float, double, char, pointer or C-structure (or union). *How functions work with parameters? Example: void testCBV(int i){ i = 69; } main(){int m; m = 0; testCBV(m); printf( "m = %d \n", m ); // prints: 0 or 69 ? } It prints 0 (zero). So testCBV works by Call By Value (κλήση κατ' αξία). In C, we have CBV also for: float, double, char, pointer and structs parameters. Example: void testCBR(int p[]){ p[0] = 69; } // now, parameter is an array main(){int pin[5]; pin[0] = 0; testCBR(pin); printf( "pin[0] = %d \n", pin[0] ); // 0 or 69 ? } It prints 69. So testCBR works by Call By Reference (κλήση κατ' αναφορά). In C, array parameters are treated by CBR. This is Very Important! How we can write function for int, float etc, that works like CBR? Answer: by pointers and addresses! Example: void intCBR (int *ip){ *ip = 69; } main(){int m; m = 0; intCBR( &m ); // pass the address of m printf( "m = %d \n", m ); // prints: 69 } Another example: Exchanging values (swap) of two variables, always needs some "help" variable, eg: help=x; x=y; y=help; Writing a function like: void BadSwap(int x, int y){int help; help=x; x=y; y=help; } doesnt work, because of CBV for int parameters! We must use pointers for function, and addresses, when calling. void GoodSwap (int *xp, int *yp) {int help; help=*xp; *xp=*yp; *yp=help; } main(){int x,y; x=5; y=10; swap( &x, &y ); // pass addresses printf( "x = %d, y = %d \n", x,y ); // prints: 10 5 }