Wiki says:
The
extern
keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable.
That means, an extern
declaration that initializes the variable serves as a definition for that variable. So,
/* Just for testing purpose only */
#include <stdio.h>
extern int y = 0;
int main(){
printf("%d\n", y);
return 0;
}
should be valid (compiled in C++11). But when compiled with options -Wall -Wextra -pedantic -std=c99
in GCC 4.7.2, produces a warning:
[Warning] 'y' initialized and declared 'extern' [enabled by default]
which should not. AFAIK,
extern int y = 0;
is effectively the same as
int i = 0;
What's going wrong here ?