It is defined, but it is defined as undefined-interesting macro definition

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.