The concept of preprocessing
The "#" begins with the preprocessing commands, which are placed outside the function in the source program, and are generally placed in front of the source file, which is called the preprocessing part . The so-called preprocessing refers to the work done before the first scan of the compilation (lexical scanning and parsing). Preprocessing is an important function of the C language, which is done by the preprocessor. When compiling a source file, the system automatically references the preprocessor to process the preprocessing part in the source program, and the processing is done automatically to the source program C language to provide a variety of preprocessing functions, such as: macro definition, File inclusion, conditional compilation, and so on. The program written by using preprocessing function is easy to read, modify, transplant and debug, and also benefit the Modularization program design.
Macros and conditional compilation:
The concept of macros
The descriptor that is defined as "macro" is called the "macro name". When compiling preprocessing, the "macro name" appearing in the program is substituted by the string in the macro definition, which is called "macro substitution" or "macro expansion" macro definition is done by the macro definition command in the source program, and the macro substitution is done automatically by the preprocessor. In the C language, "Macros are divided into parameter and no parameter" The macro name of the parameterless macro is not followed by a parameter, and the general form of the definition is: the #define identifier String # indicates that this is a pre-processing command, and all that begin with "#" are preprocessing commands define to define command identifiers for macros The defined macro name string can be a constant, an expression, a format string, and so on
Precautions:
1. The custom macro name is expressed in uppercase letters, in order to facilitate the difference with the variable, but also allows the use of lowercase letters 2. Macro definition when the macro name to represent a string, in the macro expansion and the string instead of the macro name, this is a simple substitution, the string can contain any character, can be a constant, can be an expression, The preprocessor does not make any checks on it. If there is an error, you can only find 3 when compiling the source program that has been expanded by the macro . The macro definition is not a description or statement, and you do not need to add a semicolon at the end of the line, such as a semicolon to replace 4. The macro definition must be written outside the function. It is scoped to the macro to define the command to the end of the source program. To terminate its scope, use #undef命令 5. Macro definition allows nesting #define R 4 #define PI 3.14 #define AREA R*PI// Nested definitions 6. If the macro name is enclosed in the source program, the preprocessor does not replace it with a macro substitution 7. Use macro definitions to represent data types, making writing easy #define INT int int A; a=10; The equivalent of using a macro to give int an alias #define P struct person p{ int; }; P p1= {24};
The definition method and use of the parameter macros
The C language allows macros to have parameters. The parameters in the macro definition are called formal parameters, the parameters in the macro call are called the actual parameters of the macro with parameters, in the call, not only the macro expansion, but also with the actual parameter to replace the formal parameter with parameter macro definition of the general form is: #define MACRO Name (parameter 1 , formal parameter 2 ...) the general form of a string parameter macro invocation is: macro name (argument 1, argument 2 ...); Example: #define SUM (a) a+a int result=sum (3); printf ("%d \ n", result);
Precautions for using a macro
1. With the parameter macro definition, spaces may appear between the parameters, but there can be no spaces between the macro name and the formal parameter list 2. In the parameter macro definition, the formal parameter does not allocate a memory unit, and therefore does not have to be a type definition. The arguments in the macro call are specific values. To replace the parameters with them, A type description is therefore required. This is different from the case in the function. In the function, the formal parameter and the actual parameter are two different quantities, each has its own scope, the call is to give the argument value to the formal parameter, "value passing". In the case of the parameter substitution, there is no problem with the value passing 3. The parameter in the macro definition is an identifier, and the argument in the macro invocation can be an expression of 4. In a macro definition, formal parameters within a string are usually enclosed in parentheses to avoid errors, in the macro definition in the previous example (y) * (y) The y of the expression is enclosed in parentheses, so the result is correct for example: #define SUM (x, y) (x) * (y) + (×) + (y) 5. The macro definition can also be used to define multiple statements, which are substituted into the source program in the case of a macro call : # Define M3 (M,n) m=a+2;n=a*2;
Application:
Defines a parameter macro that #define MAX (A, b) a>b?a:b
The difference between #define and typedef when defining aliases
The macro definition is simply a string substitution, the macro is done in preprocessing, and the TypeDef is processed at compile time, it is not simply substituted, but the type specifier is renamed. The named identifier has a function example of a type definition description : #define PIN1 int* typedef int *PIN2; PIN1 a,b;//Here's A is int* and b is int PIN2 a1,b1;//here a1,b1 is int* The difference is because one is the preprocessing phase, one is the compilation phase
The concepts and Benefits of conditional compilation:
1. Why use conditional compilation
1) According to different conditions to compile different program parts, resulting in different object code files. Facilitates porting and debugging of Programs 2) conditional compilation can also be implemented with conditional statements. However, the conditional statement will compile the whole source program, the production target code program is very long, and conditional compilation, Only the program Segment 1 or program segment 2 is compiled according to the conditions, and the production target program is shorter
2. #if-#else条件编译指令
Format: #if constant Expression Segment 1 #elif constant expression Program segment 2 #else program segment 3 #endif Disadvantage: Judging is based on the data in the macro definition, not in the compiler
3 #ifdef用来半段某个宏是否定义, defined as Ture
Example: int a =0; #ifdef DEBUG a =10; #else a=1000; #endif
4. #ifndef used to determine if a macro is defined, not defined as true
When using conditional compilation directives to debug a bug, similar to log management in Java
Conditions:
DEBUG1 = 1 Display debug information
DEBUG1 = 0 does not display debug information
#define Debug=1 #if debug1==1 #define LOG (format,...) printf (format,## __va_args__); #elif debug1==0 #define LOG (format,...) #else #define LOG (format,...) #endif use Log ("xxxxx-->", "ten"); Format represents formal parameters ... Indicates that a mutable parameter is also similar to a generic //# #标示可以有参数也可以没有参数 in Java, if there is no # #就至少有一个参数
C-Language Preprocessing commands