c - 如何访问被宏定义隐藏的全局变量?

标签 c macros

是否可以在全局范围内访问已在(自引用)宏中使用的变量? 我正在使用用于指代此类宏的术语。
参见 gcc.gnu.org/onlinedocs/cpp/Self-Referential-Macros.html
我的示例代码基于那里显示的示例代码。

简单示例:

#include <stdio.h>

int foo = 10; /*A variable at global scope. There may be a few of these.*/

#define foo (4 + foo) /*A self-referential macro. There may be a few of these.*/

int main()
{
    printf("foo = %d\n", foo); /*Prints 14. The value after the macro expansion */  

    /*Is there any way to access the global variable *foo* here?*/  

    return 0;
}

最佳答案

是的,您可以使用#undef foo 来删除宏定义。

关于c - 如何访问被宏定义隐藏的全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51941653/

相关文章:

这段代码可以包含缓冲区溢出吗?

macros - 如何用SPSS求所有变量的最大值和最小值并显示在表格中?

parsing - Julia 的宏可以访问嵌套函数吗?

c++ - 从命令行定义源文件的宏

迭代 undefined symbol 的宏

c++ - 如何将任意代码块作为参数传递给 C 宏

c - string 没有以 NULL 结尾,但仍然表现正常,为什么?

c - 了解开关条件下的寄存器用法

C 中的纸牌游戏。数据类型四舍五入会给出不正确的结果

c - 扫描混淆