Consider these examples:
static int a;
extern int a; //OK -- what linkage does the a have now?
static int a;
int a; //ERROR
extern int a;
static int a; //ERROR
int a;
static int a; //ERROR
extern int a;
int a; //OK as expected
int a;
extern int a; //OK as expected
Why was it OK in the first example but not in the second?
As far as file-scope variables (global-scope) are concerned, these have external linkage and a static duration when no keyword is specified.
Thank you
AFAIK, linkage and storage duration for functions is a bit different.
EDIT:
I’ve tried compiling using gcc 4.5.2 -Wall -pedantic –std=c99
More on: http://c-faq.com/decl/static.jd.html You can see that the 1st example works there too but 2nd doesn’t. However, I don’t see what makes them so different.
The answer to your first question is found in §6.2.2 of the C standard:
So the linkage of
ais internal.For your second question, the second sentence of the immediately following paragraph is apropos:
Because
ais an object, not a function, the declarationint a;with no storage-class specifier givesaexternal linkage. The same section then has this to say:Since, in your second example,
aappears with both internal and external linkage, this paragraph is triggered. One (particularly helpful) manifestation of undefined behaviour is the error that your compiler is producing.All of your examples can be understood by these rules:
int a;always declaresawith external linkage;static int a;always declaresawith internal linkage;extern int a;declaresawith whatever linkage it already had, or external linkage if it had none;ain the same scope with different linkage give undefined behaviour.