When I compile the code below
#include<stdio.h>
int main()
{
int a;
int a = 10;
printf("a is %d
",a);
return 0;
}
I get an error:
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
But if I make the variable global then it works fine.
#include<stdio.h>
int a;
int a = 10;
int main()
{
printf("a is %d
",a);
return 0;
}
Why is declaring the same global variable twice not an error, but doing that for a local variable is an error?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…