The pre-processing commands in C are uniformly defined by ansic, but they are not part of the C language itself and cannot be compiled directly because the compilation program cannot recognize them. Before compiling a program (including lexical and syntax analysis, code generation, and optimization), you must "pre-process" these special commands in the program. For example: if the program uses the # include command to include a file "stdio. h ", stdio. the actual content in the H file replaces this command. The program after preprocessing is as clean as the program without preprocessing, and then the Compilation Program will compile it to obtain the executable target code. The current compilation system includes preprocessing, compilation, and connection. We should remember that the pre-processing command is not part of the C language. It is completed by the pre-processing program before the program compilation.
C provides three main preprocessing functions: macro definition, file inclusion, and Conditional compilation. Their commands start.
I. macro definition: a specified identifier is used to represent a string. Its general form is:
# Define identifier string
# Define PI 3.1415926
We call the identifier "macro name", and the process of replacing the macro name with a string during pre-compilation is called "macro expansion", while # define is a macro definition command.
Several notes:
1. Replace a string with a macro name, that is, do a simple replacement without checking the correctness. For example, if you write 1 in the preceding example to a lower-case letter L, the precompiled program will not report an error, it is only displayed during official compilation.
2. The macro definition is not a C statement, and there is no need to add points in the row. If a semicolon is added, it will be replaced with a semicolon.
3, # The define statement appears outside the function in the program. The valid range of the macro name is to define the command and end with the source file. Generally, the # define command is written at the beginning of the file, before the function, as part of the file, it is valid within this file range.
4. Use the # UNDEF command to terminate the macro-defined scope. For example:
# Define PI 3.1415926
Main (){
}
# UNDEF pi
Mysub (){
}
In mysub, PI does not represent 3.1415926.
5. During macro definition, You can reference the defined macro names and replace them at different layers.
6. Do not replace any character in the string enclosed by double-marker in the program even if it is the same as the macro name.
7. macro definition is a special term used for preprocessing commands. Unlike definition variables, macro definition only replaces characters without memory allocation.
The macro definition with parameters not only replaces simple strings, but also replaces parameters. The general format is: # define macro name (parameter table) String
For example: # define s (a, B) a * B. The specific time is int area; Area = (2, 3 );
The macro definition with parameters is expanded and replaced as follows: if a macro with parameters (such as area = (2, 3) exists in the program )), then, replace the string specified in the # define command line from left to right. If the string contains the form parameters (such as A, B) in the macro, the related parameters (such as constants, variables, or expressions) in the Program Statement are replaced by the form parameters. If the character in the macro definition is not a parameter character (as above *), it is retained to form a replacement string.
Macros with parameters have many similarities with functions. When calling a function, they are also realistic parameters in the brackets after the function name. In addition, the number of parameters involved must be equal, however, there are major differences between them, mainly including:
1. When calling a function, the value of the real parameter expression is obtained first, and then substituted into the form parameter. The macro with the parameter is used for simple character replacement.
2. function calls are processed when the program is running, and temporary memory units are allocated for the parameters. Macro expansion is carried out before compilation. During expansion, memory units are not allocated, values are not transferred, and return values are not involved.
3. define the type of both the real parameter and the form parameter in the function. The type requirements of the two parameters are the same. If they are different, type conversion should be performed. The macro does not have a type problem, and the macro name does not have a type, its parameters are of no type and only represent a symbol. simply expand the character string specified in the ERA. When a macro is defined, a string can be any type of data.
4. Only one return value can be obtained by using a function call, and several results can be obtained by using a macro.
5. If the macro program is used for a large number of times, the source code is long after the macro program is expanded. If the macro program is not expanded once, the Program increases, but the function call does not.
6. Macro replacement does not take up the running time, but only the Compilation Time, while function calling takes up the running time (allocating units, retaining the field, passing values, and returning values ).
2. File Inclusion: one source file can include all the content of another source file, that is, other files.
# Include <File Name> or # include "file name"
It feels like a package in Java, and its function is like that in J2EE, we can use *. XML is used as the configuration file, and each module calls this file. However, if this file is modified) all the files in this file (because the original copy is used) need to be re-compiled, and it seems that the flexibility is lost.
In the # include command, file names can be enclosed by "" or <>. The difference is that when <> is used, the system stores the files to be included in the user's current directory, if not, search by Angle brackets ). Generally, if the # include command is used to include the relevant header file to call the library function, <> is used to save search time. If you want to include files compiled by the user (these files are generally in the current directory), "" is generally used. If the files are not in the current directory,.
3. Conditional compilation
Generally, all the rows in the source program are compiled. However, sometimes you want to compile a part of the content only when certain conditions are met, that is, to specify the compilation conditions for a part of the content. This is Conditional compilation.
1, # indef identifier
Procedure 1
# Else
Procedure 2
# Endif
When the specified identifier has been defined by the # include command, only program 1 is compiled during the program compilation phase; otherwise, program segment 2 is compiled.
2, # If expression
Procedure 1
# Else
Procedure 2
# Endif
Advantage: Using Conditional compilation can reduce compiled statements, thus reducing the length of the Target Program and running time. When there are many Conditional compilation segments, the length of the target program can be greatly reduced.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/vincentxu/archive/2007/11/02/1863917.aspx