#ifndef, (or #ifndef), #def, #endif等宏这几个宏是为了进行条件编译. In general, all lines in the source program are compiled. However, sometimes it is desirable to compile a part of the content only if certain conditions are met, that is, to specify the conditions for the compilation of some content, which is "conditional compilation". Sometimes, you want to compile a set of statements when a condition is met, and then compile another set of statements when the condition is not met.
The most common form of conditional compilation commands is:
#ifdef identifiers
Procedure Section 1
#else
Procedure Section 2
#endif
It does this: when the identifier is already defined (typically defined with the # define command), the program Segment 1 is compiled, or the program fragment 2 is compiled.
The #else part can also be not, namely:
#ifdef
Procedure Section 1
#denif
The "program segment" Here can be a statement group, or it can be a command line. This kind of conditional compilation can improve the generality of C source program. If a C source program is running on a system with different computer systems, there are some differences between the different computers. For example, we have a data type that should be represented in the Windows platform using the long type, whereas float is used on other platforms, which often requires the necessary modifications to the source program, which reduces the generality of the program. Can be compiled with the following conditions:
#ifdef WINDOWS
#define MYTYPE Long
#else
#define MYTYPE Float
#endif
If you are compiling a program on Windows, you can add it at the beginning of the program
#define WINDOWS
This compiles the following command line:
#define MYTYPE Long
If the following command line appears before this set of conditional compilation commands:
#define WINDOWS 0
The MyType in the pre-compiled program are replaced with float. In this way, the source program can be used for different types of computer systems without having to make any modifications. Of course, the above introduction is only a simple situation, can be based on this idea to design other conditional compilation.
For example, when you are debugging a program, you often want to output some of the required information, and no longer output it when debugging is complete. You can insert the following conditional compilation segments in the source program:
#ifdef DEBUG
Print ("Device_open (%p)/n", file);
#endif
If you have the following command line in front of it:
#define DEBUG
The value of the file pointer is output when the program runs to debug the analysis. Simply remove the Define command line after debugging. Some people may feel that without conditional compilation can also achieve this purpose, that is, in debugging, add a batch of printf statements, after debugging one after the printf statement deleted. Indeed, this is possible. However, when you add more than one printf statement when debugging, the amount of work that is modified is significant. With conditional compilation, you do not have to one by one to revise the printf statement, just delete the previous "#define DEBUG" command, then all the conditional compilation with DEBUG identifier to make the printf statement does not work, that is, the role of unified control, like a "switch".
It is sometimes used in the following form:
#ifndef identifiers
Procedure Section 1
#else
Procedure Section 2
#endif
Just the first line is different from the first: Change "ifdef" to "ifndef". Its function is: If the identifier is not defined, compile the program segment 1, or compile the program segment 2. This form is contrary to the effect of the first form.
The above two forms of use are similar, depending on the need to choose one, depending on the convenience.
There is also a form of an expression that is followed by # If, not a simple identifier:
#if an expression
Procedure Section 1
#else
Procedure Section 2
#endif
It does this by compiling program segment 1 when the specified expression value is true (not 0), or by compiling program segment 2. A certain condition can be given beforehand to enable the program to perform different functions under different conditions.
For example: Enter a line of alphabetic characters, set conditional compilation as needed, to make the letters all uppercase, or all lowercase.
#define LETTER 1
Main ()
{
Char str[20]= "C Language", C;
int i=0;
while ((C=str[i])! = '/0 ') {
i++;
#if Letter
if (c>= ' a ' &&c<= ' z ') c=c-32;
#else
if (c>= ' A ' &&c<= ' Z ') c=c+32;
#endif
printf ("%c", c);
}
}
The result of the operation is: C LANGUAGE
Now define letter 1, so that when the preprocessing condition compiles the command, because letter is true (not 0), the first if statement is compiled, and the run-time causes lowercase letters to be capitalized. If you change the first line of the program to:
#define LETTER 0
When preprocessing, the second if statement is compiled to make uppercase letters lowercase (uppercase letters with the corresponding lowercase ASCII code difference of 32). At this point the operation is:
C language
Some people will ask: without conditional compilation command and directly with the IF statement can also meet the requirements of the conditional compilation command What is the advantage? It is true that this problem can be handled completely without conditional compilation, but the target program is long (because all statements are compiled), and conditional compilation reduces the length of the target by reducing the compiled statement. When the conditional compilation segment is relatively long, the length of the target program can be greatly reduced.
#ifndef, #def, #endif等宏是什么意思 in C language