C language pre-compilation includes three types: 1. macro definition 2. file contains 3. Conditional compilation, which refers to compiling only when certain conditions are met. It has several forms:
(1) # ifdef identifier
// Program
# Else
// Program
# Endif
It means that if an identifier is defined, the execution segment 1; otherwise, the execution Segment 2
Or in the following format:
# Ifdef identifier
// Program
# Endif
# include
# include
int main(){#ifdef DEBUGprintf("debug is running\n");#elseprintf("debug is not running\n");#endif system("pause"); return 0;}
(2)
# Ifndef identifier
// Program 1
# Else
// Program 2
# Endif
It means that if the identifier is not defined, the execution segment 1; otherwise, the execution Segment 2
# include
# include
int main(){#ifndef DEBUGprintf("debug is not running\n");#elseprintf("debug is running\n");#endif system("pause"); return 0;}
(3) # if expression
// Program 1
# Else
// Program 2
# Endif
It indicates that when the expression value is true, the program segment 1 will be compiled. When the expression value is false, the program segment 2 will be compiled.
# include
# include
# define HEX 1int main(){int i=10;#if HEX==1printf("%x\n",i);#elseprintf("%d\n",i);#endifsystem("pause");return 0;}