当我编译下面的代码时
#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;
}
为什么两次声明同一个全局变量不是错误,但对局部变量这样做是错误的?