In general, all rows in the source program participate in the compilation. But sometimes it is desirable to compile a portion of the content only under certain conditions, that is, to specify a compilation condition for a portion of the content, which is conditional compilation. (conditional compile)
When conditional compilation statements are typeset, you need to consider the following three locations:
(1) There is no nesting between conditional compilation statement blocks and function definition bodies (mainly in (. h) files)
Conditional compilation keyword statement below left-aligned;
The contained #include statement (block) #define语句 (block) is even nested subordinate conditional compilation statement block, according to the layout of the block nesting to indent the layout.
(2) The conditional compilation statement block is nested outside the function body (mainly in (. c) files)
In this case, the conditional compilation statement block does not affect the function body
Conditional compilation keyword statement below left-aligned;
The contained function body definition does not need to be indented, but is still performed according to the layout method defined by a single function body.
(3) Conditional compilation statements are nested within the function body (mainly in (. c) files)
A if there is no logical path cross between the conditional compilation statement block and the statement block to which the package statement belongs, the following two ways can be
Indent layout according to block nesting method (recommended);
Conditional compilation statements do not affect the original statement block layout, and conditional compilation statements are left-aligned with the contained keyword statement blocks.
b When there is a logical path intersection between the conditional compilation statement block and the statement block to which the package statement belongs
Conditional compilation statements below left-aligned, and other statements are formatted in the normal order.
The form of conditional compilation is as follows (NNN, MMM, etc. are already defined somewhere as 1 or 0):
#if NNN
Statement1;
#elif MMM
Statement2;
#else
Statement3;
#endif
Conditional compilation directives determine which code is compiled and which are not compiled. You can determine the compilation criteria based on the value of an expression or whether a particular macro is defined.
1. #if指令
#if指令检测跟在制造另关键字后的常量表达式. If the expression is true, compile the following code until the #else, #elif或 #endif is present, otherwise it is not compiled.
2. #endif指令
#endif用于终止 #if preprocessing directives.
#define DEBUG 0
Main ()
{
#if DEBUG
printf ("debugging/n");
#endif
printf ("running/n");
}
Because the program definition debug macro represents 0, the #if condition is false and does not compile the following code until #endif, so the program directly outputs running.
If you remove the #define statement, the effect is the same.
3. #ifdef和 #ifndef
#define DEBUG
Main ()
{
#ifdef DEBUG
printf ("yes/n");
#endif
#ifndef DEBUG
printf ("no/n");
#endif
}
#if defined equivalent to #ifdef; #if!defined equivalent to #ifndef
4. #else指令
After the #if instruction is #else指令用于某个, the code behind #else is compiled when the condition of the previous #if instruction is not true. #endif指令将中指上面的条件块.
#define DEBUG
Main ()
{
#ifdef DEBUG
printf ("debugging/n");
#else
printf ("Not debugging/n");
#endif
printf ("running/n");
}
5. #elif指令
#elif预处理指令综合了 the role of #else and #if directives.
#define Two
Main ()
{
#ifdef One
printf ("1/n");
#elif defined Two
printf ("2/n");
#else
printf ("3/n");
#endif
}
The program is well understood and the final output is 2.
6. Some other standard directives
#error指令将使编译器显示一条错误信息, and then stop compiling.
#line指令可以改变编译器用来指出警告和错误信息的文件号和行号.
#pragma指令没有正式的定义. The compiler can customize its purpose. A typical use is to prohibit or allow some annoying warning messages.