c - 变量多重定义警告

标签 c mingw codeblocks

我期待以下变量 var1 的多重定义的错误消息(或至少是警告)。这意味着这不是多重定义吗?变量在 main 内部的作用域是否与它在 main 外部的作用域不同。

#include <stdio.h>
int var1;
int main (void)
{
    int var1;
    var1=12;
    printf("%d",var1);
}

最佳答案

Is the scope of the variable inside the main is different from its scope outside the main.

是的。它们是两个不同的变量。因此,不存在多变量定义错误。

使用以下程序验证它:

#include <stdio.h>

int var1;
void foo();

int main (void)
{
    int var1;
    var1=12;
    printf("%d\n",var1);
    foo();
}

void foo()
{
    var1=20;
    printf("%d\n",var1);
}

关于c - 变量多重定义警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32999029/

相关文章:

c++ - 升级到 g++ 4.7(支持 c++11): any ABI incompatibility?

windows - 在 Windows 中将 .gdbinit 放在哪里?

xcode - 如何使 Codeblocks 在 macOS big sur 上运行?

c - 通过/dev/mem读写进程内存,text段可以,data段不行,为什么?

c - 如何找到文件系统 block 大小?

python - mingw -- cl 不被识别为内部或外部命令 -- 我在新环境中的 mingw 是 true

c++ - _Unwind_resume的Mingw多重定义

c++ - 代码在 Switch 内部的函数之后不执行

c - a.out 中的内核空间

c++ - 在Solaris 9上安装Git