Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore
Introduction to preprocessing commands 1. before compiling the source program, the C language will explain some special pre-processing commands (for example, the # include file inclusion commands used previously ), generate a new source program (this process is called compilation preprocessing), and then compile it normally. to distinguish preprocessing commands from General C statements, all preprocessing commands start with the symbol "#" and end with a semicolon 3. the pre-processing command can appear in any position of the program. Its function ranges from the position where it appears to the end of the file. In practice, we try to write the pre-processing command at the beginning of the source program. In this case, its function scope is the entire source program file. 4. The pre-processing commands provided by C language mainly include:
Macro definition,
File Inclusion,
Conditional compilationThis section describes macro definitions. macro definitions can be divided into two types: macro definitions without parameters and macro definitions with parameters. 1. macro definition without parameters 1. General Form
# DefineMacro name String
For example, # define ABC 10
The string on the right can also be omitted, for example, # define ABC
2. Role
It is used to replace all "macro names" in the source program with "strings" on the right during compilation and preprocessing, and is often used to define constants.
Next, write a program to calculate the perimeter Based on the circle radius.
1 # include <stdio. h> 2 3 // All macro PI in the source program will be replaced by 3.14 during compilation and preprocessing. 4 # define PI 3.14 5 6 // calculate the perimeter 7 float according to the radius of the circle girth (float radius) {8 return 2 * PI * radius; 9} 10 11 int main () 12 {13 float g = girth (2); 14 15 printf ("perimeter: % f ", g); 16 return 0; 17}
A macro called PI is defined in row 4th. After compilation and preprocessing, the 2 * PI * radius in row 8th will become 2*3.14 * radius.
Output result:
3. usage habits and attention
1> macro names generally use uppercase letters to distinguish them from variable names, but there is no syntax error in lowercase letters.
2> do not replace macros for characters in strings expanded with double quotes in the program. For example:
1 #define R 102 int main ()3 {4 char *s = "Radio";5 return 0;6 }
A macro called R is defined in row 1st, but the 'R' in row 4th "Radio" is not replaced with 10.
3> when the macro name is replaced by a string during compilation and preprocessing, no syntax check is performed, but a simple string replacement is used. You must check the syntax of the source program that has expanded the macro name only during compilation.
1 #define I 1002 int main ()3 {4 int i[3] = I;5 return 0;6 }
During compilation and preprocessing, no matter whether the syntax is correct or not, the I of the 4th rows will be replaced with 100. However, during compilation, 4th rows of errors will be reported.
4> the valid range of macro names is from the defined position to the end of the file. To terminate the macro-defined scope, use the # undef command.
1 #define PI 3.142 /*3 .4 .5 .6 .7 */8 #undef PI
The PI macro is valid between 1st rows and 8th rows, and is invalid after 8th rows.
5> when defining a macro, You can reference the defined macro name.
#define R 3.0#define PI 3.14#define L 2*PI*R#define S PI*R*R
Ii. macro definition with parameters 1. General Form
# DefineMacro name (parameter list) String
2. Role
During compilation and preprocessing, replace all macro names in the source program with strings, and replace the parameters in the string with those in the parameter list on the right of the macro name.
1 # include <stdio. h> 2 3 # define average (a, B) (a + B)/2 4 5 int main () 6 {7 int a = average (10, 4 ); 8 9 printf ("average: % d", a); 10 return 0; 11}
A macro average with two parameters is defined in row 3rd. Row 7th is replaced with int a = (10 + 4)/2. The output result is :. Does it feel like this macro has something like a function?
3. Usage notes
1> there must be no space between the macro name and the parameter list. Otherwise, all strings after the space are replaced with strings.
1 #define average (a, b) (a+b)/22 3 int main ()4 {5 int a = average(10, 4);6 return 0;7 }
Note that there is a space between the macro name average and (a, B) in the macro definition of Line 1. Therefore, the 1st rows are changed to the following:
int a = (a, b) (a+b)/2(10, 4);
This is definitely not compiled.
2> when a macro with parameters is expanded, only simple characters and parameters are replaced, and no computation is performed. Therefore, when defining a macro, A parentheses are usually used to enclose the string parameters.
The following defines a macro D (a) to return 2 times the value of:
- If you do not need to enclose parameters in parentheses when defining macros
1 #include <stdio.h> 2 3 #define D(a) 2*a 4 5 int main () 6 { 7 int b = D(3+4); 8 9 printf("%d", b);10 return 0;11 }
Row 7th is replaced with int B = 2*3 + 4. The output result is as follows:
- If you set the macro to enclose the parameter with parentheses, change the above 3rd rows:
#define D(a) 2*(a)
Note that a on the right is enclosed in parentheses, and row 7th is replaced with int B = 2*(3 + 4). The output result is as follows:
3> it is best to enclose the calculation results in parentheses.
The following defines a macro P (a) to return the square of:
- If you do not need to enclose the calculation result with parentheses
1 #include <stdio.h> 2 3 #define Pow(a) (a) * (a) 4 5 int main(int argc, const char * argv[]) { 6 int b = Pow(10) / Pow(2); 7 8 printf("%d", b); 9 return 0;10 }
Note that the calculation results are not expanded with parentheses in the second row, but the parameters are included. The 6th line of code is replaced:
int b = (10) * (10) / (2) * (2);
After simplification: int B = 10 * (10/2) * 2; finally, the variable B is:
- If the calculation result is enclosed in parentheses
Change the above 3rd lines of code:
#define Pow(a) ( (a) * (a) )
Then the 6th rows are replaced:
int b = ( (10) * (10) ) / ( (2) * (2) );
After simplification: int B = (10*10)/(2*2);, the final output result is :. This is what we want.
5. Differences from functions
We can find that the macro definition with parameters appears in the form of functions in the source program. However, there are essential differences between the two:
1> macro definition does not involve bucket allocation, parameter type matching, parameter transfer, and return value.
2> function calls are executed when the program is running, while macro replacement is only performed during the compilation and preprocessing phase. Therefore, macros with parameters have higher execution efficiency than functions.