Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C拼图{MACRO}_C_Macros_C Preprocessor - Fatal编程技术网

C拼图{MACRO}

C拼图{MACRO},c,macros,c-preprocessor,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; } #包

我在某处遇到了下面的难题

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

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

    return 0;
}
#包括
int main()
{
{
/*在这里填写一些内容,使代码可以编译
........... 
*/   
ooo+=a;
} 
#未定义ooOoO
printf(“%d”,ooOoO);
返回0;
}

简言之,我想问一下,在printf被
#undef
加密后,如何在printf中使用
ooOoO

您需要将其声明为变量:

#define ooOoO int ooOoO = 42; int a = 1; { ooOoO
宏替换是非递归的;当替换
ooOoO
时,标识符
ooOoO
将不会被视为宏名称


如果您正在寻找不使用宏的解决方案,那么您可以忽略
#unde
指令,并且永远不要将
ooOoO
声明为宏。在C和C++中,允许代码< >
#include <stdio.h>
int main(){
{

      /*Fill in something here to make this code compile  

*/
}
int a = 0, ooOoO=0;
#define ooOoO ooOoO
{
/*
      */   
              ooOoO+=a;    
          } 
          #undef ooOoO 
          printf("%d",ooOoO); 

return 0;
}
int main(){ { /*在这里填写一些内容,使代码可以编译 */ } int a=0,ooo=0; #定义ooooooooo { /* */ ooo+=a; } #未定义ooOoO printf(“%d”,ooOoO); 返回0; } 这个怎么样

#include <stdio.h>
int main(){
    int ooOoO = 0;
    {
        int a = 3;
        ooOoO+=a;
    }
    #undef ooOoO
    printf("%d",ooOoO);

    return 0;
}
#包括
int main(){
int-ooo=0;
{
INTA=3;
ooo+=a;
}
#未定义ooOoO
printf(“%d”,ooOoO);
返回0;
}

取消定义符号到预处理器,这样它就不会被其他东西替代,但ooOoO仍然会到达编译器。

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

#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;
}
#包括
int main()
{
{
/*-插入从这里开始-*/
}
int-ooo=0,a=3;
{
/*-在此处插入端点-*/
ooo+=a;
}       
#未定义ooOoO
printf(“%d”,ooOoO);
返回0;
}

编译并打印
3

main()之后有两个大括号;这是故意的吗?我想是的。这使谜题变得更加有趣。@James这是故意的,并阻止了下面的VJo这样的琐碎解决方案。@Jim:由于块可以是空的,它实际上不会给问题增加任何东西:任何解决方案都只需要以右括号开始。@James谢谢,你说得对,在VJo的“琐碎”解决方案中添加一个}将是一个“琐碎”的解决方案修改。对不起,我放了个屁。为什么要放开头的括号?OPs问题中开始括号的数量与结束括号的数量匹配。我遗漏了什么吗?@atmoscreates:我假设(可能是错误的)在
main()
之后的第二个左大括号是一个打字错误;如果不是,那么在将
ooOoO
声明为变量之前,它引入的块无论如何都需要关闭,否则当到达
printf
语句时,它将不在范围内。我认为括号是用来。。。但你是对的。否则,这会导致变量超出范围。您似乎没有注意到末尾缺少一个
}
,因此无法编译。main之后缺少第二个左括号。当然不符合这个问题,但是OOO是如何在范围内定义的?这是其他答案提供的。当然,但这是明确要求的。James McNeils给出了解决方案,但只是在我发布后的一次编辑中解释了这一点(我还不能发表评论)。