C拼图{MACRO}

Wol*_*oks 2 c macros c-preprocessor

我在某个地方遇到了以下谜题

#include <stdio.h>
int main()
{
    {

        /*Fill in something here to make this code compile  
           ........... 
         */   
        ooOoO+=a;    
    } 
    #undef ooOoO 
    printf("%d",ooOoO); 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

总之,我想问一下,如果我ooOoO#undef编辑后如何在printf中使用它?

Jam*_*lis 11

您需要将其声明为变量:

#define ooOoO int ooOoO = 42; int a = 1; { ooOoO
Run Code Online (Sandbox Code Playgroud)

宏替换是非递归的; 在ooOoO被替换时,标识符ooOoO不会被视为宏名称.


如果您正在寻找使用宏的解决方案,那么您可以简单地忽略该#undef指令并且永远不会声明ooOoO为宏.在C和C++中允许使用#undef未定义为宏的标识符.


Atm*_*ons 6

重新格式化代码(缩进)并添加解决方案后,这就是我收到的内容:

#include <stdio.h>
int main()
{
    {
/*-Insert starts here-*/
    }
    int ooOoO = 0, a=3;
    {
/*-Insert ends here-*/
        ooOoO+=a;      
    }       
    #undef ooOoO 
    printf("%d",ooOoO);       
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译和打印 3