Introduction:At the beginning of C language learning, when we mentioned preprocessing, we thought of the # define macro definition and the # include header file. Later, with the in-depth study of C, we found that preprocessing is more than that. For example, Conditional compilation and predefined macros. The following is a summary.
Pre-processing definition: Before compiling a program, check the program by the pre-processor (so called the pre-processor) according to the pre-processor Command Used in the program, the abbreviation in the content replacement program represented by the symbolic Abbreviation used by the Preprocessor.
1. # define
The most common pre-processor command is the define command, which consists of three parts: # define itself, symbolic abbreviations, replacement list (or subject ).
The structure can be written as follows: # The macro part of the define macro subject cannot contain spaces. It can only contain numbers, underscores, and letters, but the first character cannot contain numbers.
For example: # define PX printf ("x is % d. \ n", x)
After the Preprocessor discovers a macro in the program, it replaces the macro with its equivalent replacement file. If the string contains macros, replace them. If the macro appears in double quotation marks, it is not replaced. If the macro that appears in double quotation marks has a "# operator" before it, you can use the macro parameter to create a string. For example:
# Define PSQR (X) printf ("The square of X is % d. \ n", (X) * (X )))
PSQR (8) // equivalent to printf ("The square of X is % d. \ n ", (8) * (8), output The square of X is 64.
# Define PSQR (X) printf ("The square of" # X "is % d. \ n", (X) * (X )))
PSQR (8) // equivalent to printf ("The square of 8 is % d. \ n ", (8) * (8), output The square of8 is 64.
Summary: X in the character in the quotation marks is considered as a common text, rather than a replaceable linguistic symbol. # The symbol is used as a preprocessing operator to convert the language symbol into a string.
Preprocessor adhesive: # operator. Like the # Operator above, the # operator can be used to replace class function macros. # It can also be used to replace Class Object macros and combine the two symbols into a single language symbol, for example:
# Define XNAME (n) x # n
XNAME (4) // equivalent to x4
From the definition and use of macros, we can see that they are very similar to functions, but macros and functions are different. Their choice is actually a balance between time and space. Macros generate Inline code, that is, statements are generated in the program. If a macro is used for 20 times, 20 rows of code will be inserted into the program. If a function is used for 20 times, only one copy of the function statement in the program will be copied, saving space. On the other hand, the control of the program must be transferred to the function and then return to the calling program, so this is more time than the call of the Inline code.
2. # include
After the Preprocessor discovers the # include command, it will find the file name followed by it and include the content of this file into the current file. The text of the contained file will replace the # include command in the source code file, just like typing all the content in the protected file into this specific location in the source file.
# Include // Put the file in angle brackets and search for the system working directory
# Include "name. h" // put the file name in double quotation marks and search for the current working directory
# Include "/dir1/dir2/name. h" // search/dir1/dir2 directory
Header files generally contain obvious constants, macro functions, function declarations, struct template definitions, and type definitions.
3. Other commands
# Undef command to cancel the # define definition above. # If # ifdef # ifndef # else # elif # endif command can be used to select under which code to compile. # The line command is used to reset the line and file information. # The error command is used to give an error message. # The param command is used to send an instruction to the compiler.
4. predefined macros:
_ DATE _: DATE on which preprocessing is performed
_ FILE _: String Text of the current source code FILE name
_ LINE _: an integer constant representing the row number in the current source code file
_ STDC _: indicates that the implementation follows the C standard when it is set to 1.
_ TIME _: source file Compilation TIME