Presentation is loading. Please wait.

Presentation is loading. Please wait.

ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.

Similar presentations


Presentation on theme: "ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices."— Presentation transcript:

1 ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices

2 ספטמבר 04Copyright Meir Kalech2 Arguments to main Upon program launch, main is the first function that is called. Assume we want to send parameters via the command line. For instance, a program that gets 2 strings and prints their concatenation. How to pass arguments via command line? main() can have two parameters: int argc char *argv[]

3 ספטמבר 04Copyright Meir Kalech3 Arguments to main The parameters have conventional names and their order is fixed: main(int argc, char *argv[ ]) argc - An integer specifying how many arguments are passed to the program via the command line. The program name is considered as an argument, hence argc is at least 1. argv - array of pointers to the arguments list. argv[0] is the program name. argv[argc-1] is the last argument.

4 ספטמבר 04Copyright Meir Kalech4 Arguments to main - Example main gets a string and 2 numbers and prints the string before the sum of the numbers (the execution file is called “print”). #include void main(int argc, char *argv[]) { int sum; if(argc == 4) { sum = atoi(argv[2]) + atoi(argv[3]); printf(“%d %s”, sum, argv[1]); } else puts(“required parameters missing”); } Command line> print ben 42 58 4 100 250 300 190 argc argv /0tnirp neb 24 85

5 ספטמבר 04Copyright Meir Kalech5 const Defining a value as const causes its memory to be permanently allocated with this initial value: const int x = 5; Now x can’t be changed: x = 6;// compilation error The difference between const and #define: const : a location of memory is allocated. Its scope and life time follow the rules for variables. #define : no memory is allocated. Hence, no life time. The scope is determined in pre-processing.

6 ספטמבר 04Copyright Meir Kalech6 const &parameter Two ways to pass an object to a function: 1.By value – a copy of the object is created. 2.By address – the address of the object is passed. The second way is recommended if the function needs to change the parameter. When the function does not need to change the parameter, its better to use the first way. Problem: it’s too expensive (time and place) to create a copy. Solution: pass the argument by address anyway.

7 ספטמבר 04Copyright Meir Kalech7 const *parameter Problem: dangerous!!! You may change the parameter in the function! Solution: If the function needs to change the argument, pass it by address. If the function does not change the argument, pass it: By address in order to have the efficiency. With const in order to keep it unchangeable. void func(const Object *obj); Comment: when passing primitive types, if the function doesn’t change them, it is best to send them by value since they are anyway small in size.

8 ספטמבר 04Copyright Meir Kalech8 const *parameter Const before the pointer type refers to the pointed value: void strcpy(char *destination, const char *source); The destination pointed value can be changed, but the source pointed value can’t. Const after the pointer type refers to the pointer value itself: void func(char const *source); source = (char *) malloc(10);//error

9 ספטמבר 04Copyright Meir Kalech9 Preprocessor The C preprocessor is the first to process the C source file. When we compile our code, the preprocessor processes it before the compiler. The preprocessor is no more than a simple text editor that substitutes text in our code with other requested text. It scans the source file, searching for preprocessor directives (lines that start with # ). Once a preprocessor directive is found, the preprocessor makes the relevant text substitutions and then continues to scan. Once it finishes, it passes the output to the C language compiler. In other words, the preprocessor ’ s output is the compiler ’ s input.

10 ספטמבר 04Copyright Meir Kalech10 Preprocessor - #define #define 'creates' a new token (symbolic constant) that can be used later in the source code. Instead of writing: const int num = 3; write: #define NUM 3 Wherever the token is found in the source code (excluding in strings and comments), it is replaced by the replacing string. The scope of a #define token is from the point it is declared until the end of the source file. Convention: use all capital letters for a token.

11 ספטמבר 04Copyright Meir Kalech11 Preprocessor - Macros #define with parameters is called a macro. Each occurrence of the parameter name in the macro body is replaced by the actual parameter that was ‘ sent ’ to the macro. A macro has a value, like a function, but unlike a function, a macro is not sent to the stack (we are in preprocessing stage). #define PROD(a,b) ((a) * (b)) int k=3, v; v = PROD(12, k); The preprocessor translates it to: v = ((12) * (k)); A macro can be written over a few lines, using a backslash. #define Max3(a,b,c) ((a)>(b) ? (a)>(c) ? (a) : (c) \ : (b) > (c) ? (b) : (c))

12 ספטמבר 04Copyright Meir Kalech12 Conditional Compilation Conditional compilation means that under certain conditions, the preprocessor can make some code lines ‘ disappear ’ before the compiler receives them, so that they will not be a part of the program. Motivation: there are a lot of outputs and log files that are necessary only during tests but not in the final version. Or code that differs for different platforms. We wish to ignore these in the formal version.

13 ספטמבר 04Copyright Meir Kalech13 #ifdef, #ifndef, #endif If constant_name was previously #define, then everything between #ifdef and #endif will be passed to the compiler. Otherwise, it will be not passed to the compiler. If constant_name was NOT previously #define, then everything between #ifndef and #endif will be passed to the compiler. Otherwise, it will not be passed to the compiler. #ifndef constant_name C code #endif #ifdef constant_name C code #endif

14 ספטמבר 04Copyright Meir Kalech14 Pointer to Function Assume the following problem: An array of students’ grades is defined. We want to provide the following operations: Add 5 to each one of the grades. OR: Multiply each one of the grades by 1.1. OR: Divide each one of the grades by 0.8. What is the preferred way? Define 3 functions where each one of them contains a loop. But this is in contrast to the idea of function reuse.

15 ספטמבר 04Copyright Meir Kalech15 Pointer to Function Solution: use pointer to function. We can build just one function which goes over the array elements (run_loop). Besides this function we need to build 3 more functions that get a single element and factor as parameters, and: 1.Add the factor to the element (add). 2.Multiply the element by the factor (mul). 3.Divide the element by the factor (div). The function run_loop gets a pointer to function suitable to the prototype of the above functions, and operates it via the pointer, with the element and the factor as arguments.

16 ספטמבר 04Copyright Meir Kalech16 Pointer to Function - Example void add(int *x, int factor) { *x += factor; } void mul(int *x, int factor) { *x *= factor; } void div(int *x, int factor) { *x /= factor; } void run_loop(int arr[], int factor, void (* pFunc)(int *x, int factor) ) { for(int i=0; i<3; i++) pFunc(&arr[i], factor); } Pointer to function with the prototype: Return: void Parameters: (int &, int) The same prototype

17 ספטמבר 04Copyright Meir Kalech17 Pointer to Function - Example void main() { int grades[3]={88,99,87}; run_loop(grades, 5, add); run_loop(grades, 1.1, mul); run_loop(grades, 0.8, div); } void add(int *x, int factor) {…} Address: 100 Address: 230 void mul(int *x, int factor) {…} Address: 320 void div(int *x, int factor) {…} Address: 450 889987 grades: 200 main run_loop 230200 5 arr factor pFunc void run_loop(int arr[], int factor, void (* pFunc)(int *x, int factor) ) { for(int i=0;i<3;i++) pFunc(&arr[i],factor); } add 5 x factor 200


Download ppt "ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices."

Similar presentations


Ads by Google