I have this code snippet here:
static double a; // #1
void foo (void)
{
int a = 0; // #2
{ // needs to be in its own block.
extern double a; // #3 // refers to the file scope object.
}
}
which GCC complain with:
"error: variable previously declared 'static' redeclared 'extern'"
for the line with extern double a;
marked with #3
.
and Clang complain with:
"warning: declaration shadows a variable in the global scope [-Wshadow]"
for the line with int a = 0;
marked with #2
, with reference to definition of a
at file scope at the line marked with #1
.
Now if I comment out the line marked with #2
with int a = 0;
:
// int a = 0; #2
it shows no warning and error anymore at both compilers.
It seems to have to do with that the definition of a
"shadows" the a
at file scope.
What I wonder is, that GCC seems to add the extern
ness of a
at line #3
to the file-scope a
by the shadowing of a
at line #3
by a
in line #2
.
What happens here exactly?
What does the error "error: variable previously declared 'static' redeclared 'extern'" mean exactly that I redeclare
a
at line#1
to haveextern
al linkage?
I don't understand what is going on. Shouldn't the extern
declaration of a
at the line marked #3
refer to a
at line marked #1
instead of to redeclare the linkage of a
in line #1
?
Credits:
This question is inspired by experiments at the code of Jens Gustedt's answer to Rationale of static declaration followed by non-static declaration allowed but not vice versa.