In the following program, I thought that
extern int i;
extern int i=1; // warning: 'i' initialized and declared 'extern'
extern int i; // error: extern declaration of 'i' follows declaration with no linkage
extern int i=1;
Once you define a variable named
i inside your
main function, the
i at file scope is masked and cannot be accessed (unless you have its address).
When you later add the declaration
extern int i, this conflicts with the local variable named
i at the same scope since locals can't have external linkage. It does not give you access to the global
When you remove the local
extern int i declaration matches up with the definition at file scope, so there is no error. As for the warning on
extern int i=1;, that did not go away for me on gcc 4.1.2, so that depends on the compiler.