我期待以下变量 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/