In C,why is multiple declarations working fine for a global variable but not for a local variable?
c
Solution
In C and C++, `int y;` within a function is both a declaration and a definition.
In C, `int x;` in the file scope (outside any function) is a declaration and a tentative defintion. Multiple tentative definitions are allowed; only one definition is allowed.
Problem
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). link1 link2 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); } ```