In the previous article, we mainly introduced the use of # define, which describes several of its "close relatives".
1. #undef
The C language does not allow duplicate definitions of the same macro name, such as the following compile-time error:
#define R 3 #define R 4
So#undef就是用来取消对一个宏名的定义的, we can only reuse it after cancellation.
#define R 3#undef r#define R 4
Even if R is not defined at first, the definition of cancel R is also valid. If you want to use a specific name, but you are not sure whether the name has been used before, for security reasons, you can cancel the definition of the name.
2. #ifdef #ifndef #else #endif
First, put the code:
#ifndef Header_filename#define Header_filename#endif
You can guess what this means,#ifdef means if a macro name is defined, execute the code between the command and #endif or #else, and #ifndef is the opposite, if a macro name is not defined, execute it and #endif或 # The code between else. These intermediate codes can be either pre-processing commands or normal code. such as:
#if machine = = 68000int x; #elselong x; #endif
the #ifdef #else format is very similar to the if else in C. The main difference is that the preprocessor does not recognize the curly braces {} of the tag code block , so use #else (if needed) and #endif (must exist) to mark the instruction block. These conditional structures can be nested.
In general, when a file contains several header files, and each header file may have the same macro elbow defined, using #ifndef prevents the macro from being repeatedly defined. At this point, the definition in the first header file becomes a valid definition, while the definition in the other header file is ignored. Why do you include the same file multiple times? The most common reason is that many include files themselves contain other files, and therefore may explicitly contain files that other files already contain. Why is this going to be a problem? Because some statements in a header file can appear only once in a file (such as a struct-type declaration). Standard C header Files use #ifndef technology to avoid multiple inclusions.
3. #if and #elif instructions
They are closer to normal if and else in C, followed by a constant integer expression. If the expression is a non-0 value, the expression is true. You can use the relational and logical operators of C in this expression.
Many new implementations provide another way to determine whether a name has already been defined. No need to use:
#ifdef VAX
Instead, take the following form:
#if defined (VAX)
Here, defined is a preprocessor operator, and if the defined parameter has been defined with # define, then defined returns 1, otherwise 0 is returned. The advantage of this new approach is that it can be used with #elif.
#if defined (LBMPC) <span style= "White-space:pre" ></span> #include "ibmpc.h" #elif defined (VAX) <span Style= "White-space:pre" ></span> #include "vax.h ' #elif defined (MAC) <span style=" White-space:pre "> </span> #include "mac.h" #else <span style= "white-space:pre" ></span> #include ' general.h ' #endif
4. #error #line #progma
It's too little to add later.
Conditional compilation of C language notes