Overview
We all know that a C program run consists of two stages of compiling and linking, in fact, before compiling the preprocessor first to pre-processing operations, will be processed to produce a new source file to compile. Because pre-processing directives are made before compilation, they are often more efficient than when the program is running. The C language includes three types of preprocessing directives, which are presented today:
- Macro definition
- Conditional compilation
- file contains
Macro definition
For some constants or short functions that are often used in programs, we usually use macro definitions to handle them, and the benefit is that all configurations in the program can be managed uniformly in the macro definition, and because the macro definition is more efficient than defining a global variable or function before the program compiles.
////main.c//pretreatment////Created by Kenshin Cui on 14-6-28.//Copyright (c) 2014 Kenshin Cui. All Rights reserved.//#include<stdio.h>#definePI 3.14//Macro definition general capitalization#defineR Ten#defineS 2*pi*r//The above macro is referenced in another macrointMain (intargc,const Char* argv[]) { floatr=10.5; DoubleArea=pi*r*r; printf ("area=%.2f\n", area); Doublea=s; printf ("a=%.2f\n", a); printf ("pi=3.14\n");//Note that the output is not 3.14=3.14 but pi=3.14, and the pi in the string is not replaced#undefPI//force terminate macro definition, otherwise it's range until end of fileintpi=3.1415926; DoubleArea2=pi*r*r; printf ("area2=%.2f\n", AREA2); return0;}
The actual operation of a macro definition is to replace it during preprocessing, regardless of whether the syntax is correct, and the macro name that appears in the string is not replaced. The functions defined by macros are in fact very powerful, but you can also pass in parameters in addition to simple constant substitution:
////1.2.c//pretreatment////Created by Kenshin Cui on 14-7-17.//Copyright (c) 2014 Kenshin Cui. All Rights reserved.//#include<stdio.h>#defineSUM (A, b) a+b#defineSUB (A, a) (A-B )#defineMUL (A, B) (a*b)//This definition is wrong, the preprocessor will consider the macro name "MUL" and replace it with "(A, B) (a*b)"intMain (intargc,const Char* argv[]) { inta=2,b=3,c,d; C=sum (A, b); printf ("c=%d\n", c); //Result: c=5D=sum (A, b) * * *; printf ("d=%d\n"); //Result: 8, why not 10? Because after replacement: D=a+b*2 is also 2+3*2=8intE=sub (b, a) * * *; printf ("(b-a) *2=%d\n", E); //Result: 2, if sub is defined without parentheses here should be -1return0;}Above we can see the macro function with parameters is very powerful, a bit similar to the function, the same function is just a simple substitution, does not involve storage space allocation, parameters, return values and other problems, but because it is in the preprocessing phase of the expansion, so the general efficiency is high. Using a macro with parameters is important to note that the result is best enclosed in parentheses, otherwise it is very easy to see the problem (as we should have seen in the sum above), and one thing is that the macro definition with the parameter does not have a space between the name and the parameter. Conditional compilation
Conditional compilation is actually before compiling the preprocessor according to pre-processing instructions to determine the corresponding conditions, if the condition satisfies the corresponding code is compiled into, otherwise the code will not enter the compilation link (equivalent to no such code at all).
////main.c//pretreatment////Created by Kenshin Cui on 14-06-28.//Copyright (c) 2014 Kenshin Cui. All Rights reserved.//#include<stdio.h>#defineCOUNT 1intMain (intargc,const Char* argv[]) { //Determines whether the COUNT macro is defined#if defined(COUNT)//equivalent to: #ifdef count, on the contrary if the judgment is not defined, it can be passed by # if!defined (count) or #ifndef countprintf ("COUNT defined\n");#endif//Determine if the macro defines whether the count is all 1#ifcount==1 ShowMessage ("hello,world!\n");#elsesay ();#endif return0;}file contains
The file contains directive # # has been used many times before, and here again. The first use of # include "XXX" contains and uses # include <xxx> contains the difference is that when using <> include, the preprocessor will search the C function library header file path under the file, while using "" contains the first search for the directory of the program, Second, search the system path definition directory, if still cannot find the C function library header file in the same directory.
In addition, when using # include, we need to be aware that the inclusion of the file is not recursive, for example, the A.h file contains B.h, and b.h can no longer contain a.h, and there is a repeat contains although it is allowed but this will reduce the performance of the compilation, you might want to look at the following example:
There are three pieces of code above, both MAIN.C and Person.h contain message.h and main.c itself contains Person.h, so the program will replace the included content in the preprocessing phase, and the MIAN.C contains two # include " Message.h "Although there is no error, but this will affect the performance of the compilation, the correct approach should be:
In fact, the macro definition is used to determine whether a macro is defined, if there is no definition of the macro will be defined, so that if it has been included in the macro definition has been defined, even if the inclusion is not redefined, the following code will not be included in.