1. Conditional compilation
Conditional compilation means that preprocessing is included or excluded based on the test results.Program.
2. # If and # endif
Let's not talk about it. Let's just give an example.
# DefineDebug 1IntMain (Void){# IfDebug printf ("Hello World");# Endif return0 ;}
In fact, the effect is similar to thisCode:
IntMain (Void){IntDEBUG = 1;If(Debug) printf ("Hello World");Return0 ;}
The difference is that pre-processing commands are processed by the pre-processor. # The if command tests the debug value. If the debug value is 0, the printf will not save the space occupied by the target program, nor will it consume the running time of the program. Therefore, we can save this code to the final code during the test.
3. Compile symbols with similar conditions
The defined operator is usually used together with # If to determine whether a macro definition is defined.
# DefineDebug 1IntMain (Void){# If definedDebug printf ("Hello World");# Endif return0 ;}
The same semantics can also be written as follows:
# DefineDebug 1IntMain (Void){# IfdefDebug printf ("Hello World");# Endif return0 ;}
Of course, # ifndef stands for the semantics of if not defined.
# Elif: else if
# Else.
These can be used together with # If, and are the same as general condition judgment semantics. I will not repeat the example.
4. Usage of Conditional compilation
A. debugging.
B. Compile programs used by different operating systems, platforms, and compilers.
For example:
# DefineWindowsIntMain (Void){# If definedWindows printf ("Windows");# Elif definedLinux printf ("Linux");# ElsePrintf ("Else");# Endif return0 ;}
C. Temporarily shield code
5. # error command
# Error indicates a serious error. The Compiler immediately terminates the program. For example:
IntMain (Void){# If definedWindows printf ("Windows");# Elif definedLinux printf ("Linux");# Else # errorNot support# Endif return0 ;}
6. Managed and independent implementations
This is a basic concept.
Most programs are managed. These programs require the underlying operating system to provide input and output and some other basic services. The independent implementation of C is used for programs that do not require an operating system.
In general, standalone programs are used to write the operating system kernel and embedded programs.