【发布时间】:2014-02-12 02:26:07
【问题描述】:
当我编译下面的代码时
#include<stdio.h>
int main()
{
int a;
int a = 10;
printf("a is %d \n",a);
return 0;
}
我收到一个错误:
test3.c: In function ‘main’:
test3.c:6:5: error: redeclaration of ‘a’ with no linkage
test3.c:5:5: note: previous declaration of ‘a’ was here
但如果我将变量设为全局变量,那么它就可以正常工作。
#include<stdio.h>
int a;
int a = 10;
int main()
{
printf("a is %d \n",a);
return 0;
}
为什么声明同一个全局变量两次不是错误,而对局部变量这样做却是错误?
【问题讨论】:
-
this question答案的最后一部分。
-
@downvoters 愿意发表评论。如果您对重复投反对票,请投赞成票,不要投反对票。
标签: c declaration definition redefinition one-definition-rule