这是我的代码,我想打印15和12,但由于实例成员隐藏了a的本地值,因此将打印两次。

#include <stdio.h>
int a = 12;
int main()
{
    int a = 15;
    printf("Inside a's main local a = : %d\n",a);
    printf("In a global a = %d\n",a);
    return 0;
}

为什么有办法用C语言打印出来?…顺便说一句,我是用C++写的。

最佳答案

在新的复合语句中使用extern说明符。
这种方式:

#include <stdio.h>

int a = 12;

int main(void)
{
    int a = 15;
    printf("Inside a's main local a = : %d\n", a);

    {
        extern int a;
        printf("In a global a = %d\n", a);
    }

    return 0;
}

关于c - 如何打印具有相同名称的全局变量和局部变量的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12183899/

10-12 00:33
查看更多