Some days ago I wrote a small program to find out the macro definition. I suddenly found that defining the identifier as null is totally different from defining the identifier as undefined ...... Then we found that there is another state in the original macro definition called "undefined ". The following is the small program and simple analysis. Through this program, you can fully experience the "undefined" status and try out what is "It is defined, but it is defined as undefined ".
Program source code:
#include<stdio.h>int main(){#if (A == B) printf("define A = B\n");#elif (A == C) printf("define A = C\n");#else printf("nothing is defined!\n");#endif return 0;}
The following is the compilation command and running result:
[Root @ localhost test] # GCC definetest. C-o definetest
[Root @ localhost test] #./definetest
Define a = B
[Root @ localhost test] # GCC definetest. C-o definetest-da
[Root @ localhost test] #./definetest
Nothing is defined!
[Root @ localhost test] # GCC definetest. C-o definetest-da = C
[Root @ localhost test] #./definetest
Define a = B
[Root @ localhost test] # GCC definetest. C-o definetest-da = C-DC
[Root @ localhost test] #./definetest
Define a = C
[Root @ localhost test] # GCC definetest. C-o definetest-da = C-DB
[Root @ localhost test] #./definetest
Define A = C
First, let's briefly introduce the meaning of-D during gcc compilation.-D is actually macro definition during preprocessing, which is the same as # define implementation in code. What follows D is the macro definition:-DA, which is equivalent to # define A in the Code; and-DA = C is equivalent to # define a c in the code.
The first is the simplest compilation method, which is not defined at all. But it was executed in the first branch. Why? Because at this time A has not been defined, B has not been defined, so undefined is equal to undefined, so it is executed in the first branch.
Then how can we proceed to the third branch? The second compilation method is basically the simplest solution. Macro defines A as null, and then A is defined as null, while B and C are still undefined, so they will go to the third branch.
The third case should be the most interesting one. We generally think that the program should be executed to the second branch and print define A = C, but it is still implemented in the first branch. In fact, this is the situation where "it is defined, but it is defined as not". A is defined as C, but C is "undefined ", so A becomes "undefined", and B is "undefined", so A is the same as B, so it is implemented in the first branch.
Then, if C is defined, or B is defined, as long as both are not "undefined", it will enter the second branch of the program. Corresponding to the fourth and fifth compilation methods respectively.