Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22271

Why does GCC saying that I attempt to modify the linkage of a variable at file scope when shadowed? [duplicate]

$
0
0

I have this code snippet here:

static double a;          // #1void 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 #3.

Online Example


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 externness 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?

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?

If possible, quotations from C standard are highly appreciated.


Credits:

This question is created 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.


Viewing all articles
Browse latest Browse all 22271

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>