I know a variable with initialization is a definition no matter if it is extern
. However, since extern int x=1;
is a definition, why doesn't the compiler throw redefinition error running the following code?
//ok,no error
int x;
extern int x=1;
int main()
{
}
//however,this is an error
extern int x=1;
int x;
int main()
{
}
Why can this happen? Is there any difference?
Update:You are right.When I compile with VS 2013,there is no error.Now I tried with gcc on linux and got an redefinition error just as I expected.
Just as @Bo Persson said,it's all about tentative definition in C. And when I change the file suffix to .c, gcc takes it as a warning rather than an error and it can be compiled.