C谜题{宏}
我在某处遇到了以下难题
#include <stdio.h>
int main()
{
{
/*Fill in something here to make this code compile
...........
*/
ooOoO+=a;
}
#undef ooOoO
printf("%d",ooOoO);
return 0;
}
简而言之,我想问在 printf 被 #undef
编辑后,如何在 printf 中使用 ooOoO
?
I came across the following puzzle somewhere
#include <stdio.h>
int main()
{
{
/*Fill in something here to make this code compile
...........
*/
ooOoO+=a;
}
#undef ooOoO
printf("%d",ooOoO);
return 0;
}
In short I want to ask how can I use ooOoO
in printf after it has been #undef
ed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要将其声明为变量:
宏替换是非递归的;当
ooOoO
被替换时,标识符ooOoO
将不会被视为宏名称。如果您正在寻找不使用宏的解决方案,那么您可以简单地忽略
#undef
指令,并且永远不要将ooOoO
声明为宏。在 C 和 C++ 中都允许使用#undef
未定义为宏的标识符。You need to declare it as a variable:
Macro-replacement is non-recursive; while
ooOoO
is being replaced, the identifierooOoO
will not be treated as a macro name.If you are looking for a solution that does not use a macro, then you can simply ignore the
#undef
directive and never declareooOoO
as a macro. It is permitted in both C and C++ to#undef
an identifier that is not defined as a macro.重新格式化代码(缩进)并添加解决方案后,这就是我收到的内容:
编译并打印
3
After reformatting the code (indent) and adding the solution, that's what I receive:
compiles and prints
3
这个怎么样?
How about this?
#undef 取消对预处理器的符号定义,以便它不会被其他东西替换,但 ooOoO 仍然到达编译器。
The #undef undefines the symbol to the preprocessor so that it does not get substituted with something else, but ooOoO still gets to the compiler.