Block Communication C

The following identifiers are not bound: an identifier declared as something other than an object or function; identifier declared as a function parameter; block area identifier for an object declared without an extern storage class specifier .

{ static int a; //no linkage } 

For an identifier declared using the extern storage class specifier in the scope in which a preliminary declaration of this identifier is visible, if the previous declaration indicates an internal or external link, the link of the identifier with the subsequent declaration is the same as the link specified in the previous declaration. If no previous announcement is displayed, or if no link is specified in the previous announcement , then the identifier has an external link .

 { static int a; //no linkage extern int a; //a should get external linkage, no? } 

GCC error: extern declaration of next declaration without binding

Can someone explain to me why I get this error?

thanks

+6
source share
2 answers

Your assumption is correct: the second declaration a has an external connection. However, you get an error because your code violates the restriction in ยง6.7:

3 If the identifier has no connection, there should be no more than one declaration of the identifier (in a pointer or type specifier) โ€‹โ€‹with the same scope and in the same namespace, except for tags as specified in clause 6.7.2.3.

That is, once you have declared a to have no connection, you cannot reuse it in the same area.


A valid example of this rule is:

 int a = 10; /* External linkage */ void foo(void) { int a = 5; /* No linkage */ printf("%d\n", a); /* Prints 5 */ { extern int a; /* External linkage */ printf("%d\n", a); /* Prints 10 */ } } 
+8
source

if the previous declaration does not indicate a binding

means

if the anchor sign is not specified in the previous announcement

but not

if the previous ad indicates that it has no binding

This is confusing and ambiguous; not an ordinary way to write a standard ...

+2
source

Source: https://habr.com/ru/post/896183/


All Articles