C language-07 others,-07 others
Preprocessing commands
/* Macro definition without parameters 1. all preprocessing commands start with #. 2. preprocessing commands are divided into three types: 1. macro definition 2. Conditional compilation 3. File Inclusion 3. the pre-processing command is executed before the code is translated into 0 and 1. the pre-processing position is random. scope of the pre-processing instruction: Starting from the line where the instruction is written to the end of the file, you can use # undef to cancel the macro definition. 6. macro names generally start with uppercase or k, and variable names are generally different from variables in lower case */# include <stdio. h> // # define kCount 4int main () {char * name = "COUNT"; // printf ("% s \ n ", name); # define COUNT 4 int ages [COUNT] = {1, 2, 67, 89}; for (int I = 0; I <COUNT; I ++) {printf ("% d \ n", ages [I]);} // starting from this line, the COUNT macro becomes invalid # undef COUNT int a = COUNT; return 0 ;}
/* 1. macro definitions with parameters are more efficient than functions * // * int sum (int a, int B) {return a + B;} */# include <stdio. h> # define sum (v1, v2) (v1) + (v2) # define pingfang (a) * ()) // note that to calculate the correct parameters and result values, you must add parentheses. The macro is only responsible for replacing the result. The result is calculated at run time. If the priority problem occurs, it will be calculated incorrectly without parentheses. (*,/) int main () {// pingfang (5 + 5) (10*10) // pingfang (5 + 5) // pingfang (5 + 5) (35) // pingfang (5 + 5)/pingfang (2) int c = pingfang (5 + 5)/pingfang (2); printf ("c is % d \ n ", c);/* int c = sum (2, 3) * sum (6, 4 ); // note that if the value following the macro definition is incorrectly calculated without parentheses, 2 + 3*6 + 4 = 24 should be 50 macros with parentheses (2 + 3) * (6 + 4) printf ("c is % d \ n", c); * // * int a = 10; int B = 20; int c = sum (a, B); printf ("c is % d \ n", c); // int c = sum (a, B ); */return 0 ;}