#define IDENTIFIER without a token

前端 未结 4 532
长发绾君心
长发绾君心 2020-12-06 00:05

What does the following statement mean:

#define FAHAD

I am familiar with the statements like:

#define FAHAD 1
4条回答
  •  遥遥无期
    2020-12-06 00:40

    Any #define results in replacing the original identifier with the replacement tokens. If there are no replacement tokens, the replacement is empty:

    #define DEF_A "some stuff"
    #define DEF_B 42
    #define DEF_C
    printf("%s is %d\n", DEF_A, DEF_B DEF_C);
    

    expands to:

    printf("%s is %d\n", "some stuff", 42 );
    

    I put a space between 42 and ) to indicate the "nothing" that DEF_C expanded-to, but in terms of the language at least, the output of the preprocessor is merely a stream of tokens. (Actual compilers generally let you see the preprocessor output. Whether there will be any white-space here depends on the actual preprocessor. For GNU cpp, there is one.)

    As in the other answers so far, you can use #ifdef to test whether an identifier has been #defined. You can also write:

    #if defined(DEF_C)
    

    for instance. These tests are positive (i.e., the identifier is defined) even if the expansion is empty.

提交回复
热议问题