#if vs. if
Conditional compilation is the content of the preprocessing section in C language, which is the first part of the compiler's compilation of code.
Conditional compilation contains judgment statements, such as #if, #else, #elif, and #endif
It means that if the macro condition conforms, the compiler compiles the code, otherwise the compiler ignores the code without compiling it, as
#define a 0 //define a as 0
#if (a > 1)
printf ("a > 1"); The statement is not compiled by the compiler, which does not generate assembly code
#elif (A = = 1)
printf ("A = = 1");//The compiler does not compile the statement, which does not generate assembly code
#else
printf ("A < 1 "); The compiler compiles the code and generates the assembly code to execute the statement
#endif
If the IF statement is not, if is the keyword in C language, it is based on the expression of the calculation of the execution of the statement, which each of the branches are compiled, such as
#define A 0
if (a > 1)
printf ("a > 1"); The compiler compiles the statement, but because a = = 0 does not execute
else if (a = = 1)
printf ("A = = 1"); The compiler compiled the statement, but because A = = 0 was not executed
else
printf ("A < 1"); The compiler compiles the statement because A = = 0 executes
As a compile "switch", for example:
#if (condition satisfied)
executes code 1
#else
executes code 2
#endif
If the condition is true at compile time, the generated program file (. exe file) will not have code execution 2. If you use the normal if statement, the generated program file will have execution code 2, which is the difference between the size of the generated file. If your condition has been determined before the program is compiled, use #if, or if the condition needs to be judged by the program's running process.
So simply put, conditional compilation is a selective compilation of statements based on macro conditions, which is done when the compiler compiles the code;
A conditional statement selectively executes a statement based on a conditional expression, which is performed when the program is running. #if的使用说明
#if的后面接的是表达式
#if (max==10) | | (max==20)
Code ...
#endif
Its function is: if (max==10) | | (MAX==20) is established, then the compiler will put the #if and #endif之间的代码编译进去 (note: is compiled, not executed. ) #if The use of defined
#if后面接的是一个宏.
#if defined (x) ...
code ...
#endif
This #if defined it doesn't matter whether the logic inside the "X" is "true" or "false" it just doesn't have a macro defined "X" in the previous macro definition of the program. If x is defined, the compiler compiles the middle ... code ... Otherwise do not directly ignore the middle of ... code ... Code.
In addition #if defined (x) can also be reversed and used #if!defined (x) #ifdef的使用
Consistent use of #ifdef的使用和 #if defined ()
The usage of the #ifndef又和 #if!defined () is consistent.
Finally emphasize two points:
First: These macro definitions simply determine whether the code block is compiled.
Second: Don't forget the #endif