C language NOTE-Based Conditional compilation
The previous article mainly introduced # define usage. This article describes several of its close relatives ".
1. # undef
The C language does not allow repeated definitions of the same macro name. For example, an error is reported during compilation in the following statement:
#define R 3 #define R 4
Then # undef is used to cancel the definition of a macro name. After cancellation, we can use it again.
#define R 3#undef R#define R 4
Even if R is not defined at the beginning, it is legal to cancel the definition of R. If you want to use a specific name, but you cannot determine whether the name has been used before, you can cancel the definition of the name for security reasons.
2. # ifdef # ifndef # else # endif
First put the code:
#ifndef HEADER_FILENAME#define HEADER_FILENAME#endif
You can see what the name means. # ifdef indicates that if a macro name is defined, the code between executing this command and # endif or # else is the opposite of # ifndef, if a macro name is not defined, run the code between it and # endif or # else. The intermediate code can be either a preprocessing command or a common code. For example:
#if MACHINE == 68000int x;#elselong x;#endif
# Ifdef # The else format is very similar to if else in C. The main difference is that the pre-processor cannot identify the curly braces {} that mark the code block. Therefore, use # else (if needed) and # endif (must exist) to mark the instruction block. These condition structures can be nested.
Generally, when a file contains several header files, and each header file may have the same macro elbow, use # ifndef to prevent repeated macro definition. In this case, the definitions in the first header file become valid definitions, while those in other header files are ignored. Why does it contain the same file multiple times? The most common cause is that many include files themselves that contain other files, so they may explicitly include files already contained by other files. Why is this a problem? This is because some statements in the header file can only appear once in the-file (such as schema Declaration ). The # ifndef technology is used in the standard C header file to avoid multiple inclusion.
3. # if and # elif commands
They are closer to if and else in General C, followed by constant integer expressions. If the expression is a non-zero value, the expression is true. In this expression, you can use the relational and logical operators of C.
Many new implementations provide another method to determine whether a name has been defined. Not required:
# Ifdef VAX
Instead, the following format is used:
# If defined (VAX)
Here, defined is a Preprocessor operator. If the defined parameter has been defined by # define, defined returns 1; otherwise, 0. The advantage of this new method is that it can be used with # elif.
#if defined (lBMPC)#include "ibmpc.h'#elif defined (VAX)#include "vax.h'#elif defined (MAC)#include "mac.h"#else#include 'general.h'#endif
4. # error # line # progma
In the future, I will try to add too little.