Preprocessing command, preprocessing
Reprinted from: http://www.cnblogs.com/kykuaileren/archive/2011/09/09/2172801.htmlThe pre-processor will analyze and execute all the pre-processing commands based on the pre-processing commands that have been placed in the file to modify the content of the source file, and then delete them, A conversion unit containing only C ++ statements is obtained. The pre-processing command starts "#".
Common preprocessing commands:
1. # include header files (header files generally contain function prototypes and global variables)
The following two forms are common:
# Include <iostream> |
Reference the standard header file and only search for the default directory of the standard library header file |
# Include "myheader. h" |
Reference the custom header file, search for the Directory of the source file being compiled, and search for the default directory containing the standard library header file when it is not found |
2. # define defining symbols and macros
For example: # define PI 3.1415925 // defines the symbol constant PI as 3.1415925 without any additional points.
Before compilation, the pre-processor will traverse the code and replace it with the defined value (3.1415925) of the string PI where it deems the replacement meaningful. The PI in the annotation or string is not replaced.
# Undef deletion # define-defined symbols
For example:
# Define PI 3.14159265
......
// All PI values can be replaced with 3.14159265
# Undef PI
// No longer has the PI identifier
However, in C ++, it is best to use const to define variables. The preceding definition can be changed:
Const long double PI = 3.1415925;
In comparison, the former has no type designation, which may cause unnecessary troubles, while the latter has a clear definition. Therefore, we recommend using const to define constants in c ++.
# Define disadvantages:
1) type check is not supported
2) scope is not considered.
3) the symbol name cannot be limited to a namespace.
3. # indef, # ifndef, # endif
# Ifdef |
Compile the macro if it is defined. |
# Ifndef |
Compile the macro if it is not defined. |
# Endif |
End Block Control |
4. # if, # else
# If |
Code is compiled if the expression is not zero. |
# Else |
Compile the Code as an if false option |
5. # error
Output an error message
6. # pragma
Provides unconventional Control Flow Information for compilation programs