I canโt understand why this is not working.
extern int i; int main() { printf(" %d ", i); } static int i =3;
Also, this does not work:
extern int i; static int i =3; int main() { printf(" %d ", i); }
But if the static variable is defined before the extern declaration , it works:
static int i =3; extern int i; int main() { printf(" %d ", i); }
As I understand from extern int i says that i present somewhere else , and here is what it looks like ( int i )
But somewhere else means:
1) Perhaps later, specify in the translation block the same as global variable .
2) Perhaps in some translational unit other .
I thought that (1) would be valid even if static int i = 3 limited area i current translation unit where it is defined.
Not static int i =3 global (I mean, at least it can be seen in the translation block), although it has a limited scope for its translation unit? Then why can't the compiler find it?
When compiling the first two versions, I get the following compile time error:
error: static declaration of 'i' follows non-static declaration note: previous declaration of 'i' was here
I can not understand this error message. Also, why does he complain about it as a static declaration and not a definition ?