In the following code, why do multiple declarations (and one definition) work fine for a global variable x
but not for a local variable y
which is inside the main()
function? It shows the following 2 errors:
1) redeclaration of 'y' with no linkage
2) previous declaration of 'y' was here
Why is it showing error for the local variable but not global variable? Not only my book, but the following 2 links from this forum clearly state that we can declare a variable multiple times (though define only once).
And kindly take care to explain what does the "with no linkage" part of the first error "redeclaration of 'y' with no linkage" mean? What linkage and to whom? Where would a local variable be linked?
#include<stdio.h>
int x;
int x;
int x=303;
int main(void)
{
int y;
int y;
int y=776; //Works fine if above 2 declarations are removed!!
printf("The value of x is %d,and of y is %d",x,y);
}