Can you ellobrate more on your questions? because in the very first care you commented OK but that produces an error saying "static declaration of ‘a’ follows non-static declaration"
–
user919065Aug 30 '11 at 5:56

1 Answer
1

The answer to your first question is found in §6.2.2 of the C standard:

4 For an identifier declared with the storage-class specifier extern
in a scope in which a prior declaration of that identifier is visible,
if the prior declaration specifies internal or external linkage, the
linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is
visible, or if the prior declaration specifies no linkage, then the
identifier has external linkage.

So the linkage of a is internal.

For your second question, the second sentence of the immediately following paragraph is apropos:

5 If the declaration of an identifier for a function has no
storage-class specifier, its linkage is determined exactly as if it
were declared with the storage-class specifier extern. If the
declaration of an identifier for an object has file scope and no
storage-class specifier, its linkage is external.

Because a is an object, not a function, the declaration int a; with no storage-class specifier gives a external linkage. The same section then has this to say:

7 If, within a translation unit, the same identifier appears with both
internal and external linkage, the behavior is undefined.

Since, in your second example, a appears 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 declares a with external linkage;

static int a; always declares a with internal linkage;

extern int a; declares a with whatever linkage it already had, or external linkage if it had none;

Two declarations of a in the same scope with different linkage give undefined behaviour.

I wonder, what is the rationale behind external inheriting linkage? As for me, it only makes confusion.
–
mindlessAug 30 '11 at 6:15

"if the prior declaration specifies internal or external linkage". Does this mean it should be explicitly specified or that it can also be implicitly determined like it was mentioned in your second paragraph: "If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern".
–
mindlessAug 30 '11 at 6:25

@mindless: I don't think it matters - that is the int a; extern int a; case. Either the extern int a; declaration has external linkage because the int a; declaration has external linkage, or it has external linkage because the int a; declaration (directly) specified no linkage - no matter what interpretation you use, the result is the same. By the way, the first sentence you have quoted of paragraph 5 is specifically about functions.
–
cafAug 30 '11 at 6:47

It probably a problem with me and my comprehension of English or everything in general, but considering this case: { int a; extern int a; } how could standard be interpreted. I might open another question so I could understand ambiguities. Just now I understood that keyword and linkage is not the same (function example). It gives keyword but does not mention final linkage.
–
mindlessAug 30 '11 at 6:54

@mindless: In the case of int a; extern int a; (at file scope) both declarations of a have external linkage - I don't see what the problem is.
–
cafAug 30 '11 at 7:37