First, the origin of pretreatment: There are many language characteristics (especially the obscure language of the language) in the history of C , and the pre-treatment is one of them. C inherits the C language pre-processor from the C language (C language pre-processor, by Dr. Bjarne is referred to as CPP, I don't know if it is referred to as C Program Preprocessor). Second, the common pretreatment function: The main role of the preprocessor is to replace a resource through the preprocessing built-in function. The most common preprocessor is: file containing, conditional compilation, layout control and macro replacement . The file contains: #include is a most common preprocessing, mainly to be a reference combination source program body. Conditional compilation: # if, # iFNDef, # ifdef, # Endif, # undef, etc. is also a common pre-process, mainly for compile time with selective selection, comment down some specified code to achieve version control, preventive The file repeatedly included. Layout control: #pragma, which is also an important aspect of our application pretreatment, the main function is to provide unconventional control flow information for compilers. Macro replacement: #define, this is the most common usage, it can define various functions such as symbol constants, function functions, renamed, string splicing. Third, pre-processing instructions: The format of the pre-processed instruction is as follows: # Directive tokens # symbols should be the first non-empty character of this line, usually we put it in the starting position.
If the instruction can't put it, it can be controlled by / control, for example: #define error if (error) exit (1) equivalent to #define error / if (error) exit (1) but we are generally not very so used, the more common as follows: # ifdef __BORLANDC__ if_true <(is_convertible
Fourth, the file contains instructions: This pre-processing method is the most common, usually we write programs, the most common usage is: #include
Using the format, as follows: 1, #ifdef identifier your code #ENDIF If Identifier is a defined symbol, your code will be compiled, otherwise the 2, #ifndef Identifier your code #Endifier if Identifier is an undefined symbol, Your code will be compiled, otherwise it will be removed 3, #IF expression your code #ENDIF If the expression is not zero, your code will be compiled, otherwise the excuse 4, #ifdef Identifier your code1 #Else your code2 #ENDIF If Identifier is a definition The symbol is compiled. Otherwise, Your Code2 will be compiled 5, #IF expressin1 your code #elif expression2 your code #Else Your code3 #ENIF If Epression1 is non-zero, compile your code1, otherwise, if expression2 Non-zero, compile your code2, otherwise, compile your Code3 other precompiled instructions In addition to the concentrated compilation instructions we said above, there are three uncommon compilation instructions: # line, # error, # pragma, we Next, let's talk about it. #LINE The syntax is as follows: #LINE Number filename, for example: #LINE 30 a.h where the file name A.H can be omitted. This instruction can change the current line number and file name, such as the above pre-processing instruction, can change the current line number 30, and the file name is A.H. It seems that there is nothing to use, but he is still a bit used, that is, in writing in the compiler, we know that the compiler will generate some intermediate files in the C source compilation process, and can guarantee files through this directive. The name is fixed and will not be replaced by these intermediate files, which is conducive to analysis. #Error syntax is as follows: #error info, for example: #ifndef unix #error this Software Requires the UNIX OS. #ENDIF This instruction is mainly to give an error message. The above example is that if not in the UNIX environment, it will output This Software Requires The UNIX OS. Then induce the compiler to terminate. Therefore, the purpose of this instruction is to give certain information before the program crashes.