C communication confusion

I am a C programmer. I go through a simple piece of code in C

int a ; // A const int b; // B static int c; //C void func(int d) // D { //..... } 

What is the relationship between the variables a, b, c and d. I am pretty sure that a by default has an external binding, b , c and d have an internal binding. Do I understand correctly?

This is my first question on this site.

+4
source share
4 answers

From section 6.2.2, Linkages of identifiers , C99, final source:

1 / An identifier declared in different areas or in the same area more than once may be used to refer to the same object or function to a process called binding. There are three types of communication: external, internal and not one.

2 / In the set of translation units and libraries that make up the entire program, each declaration of a specific identifier with external communication indicates the same object or function. Within a single translation unit, each identifier declaration with an internal link identifies the same object or function. Each id declaration without a reference identifies a unique object.

3 / If the declaration of the file area identifier for an object or function contains a static storage class specifier, the identifier has an internal binding.

4 / 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 identifier link with the subsequent declaration is the same as the link specified in the previous declaration. If no preliminary announcement is displayed or if no binding is specified in the previous announcement, then the identifier has an external binding.

5 / If the identifier declaration for the function does not have a storage class specifier, its binding is defined exactly as if it were declared using the extern storage class specifier. If an identifier declaration for an object has a file scope and a storage class specifier, its relationship is external.

6 / 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.

7 / If the same identifier with internal and external relationships appears inside the translation unit, the behavior is undefined.

Now, going to your variables one by one:

  • a considered in part 5, as it is "an identifier for an object [which] has a file scope and a storage class specifier". Therefore, he has an external connection.
  • b also discussed in Part 5 (file area, without storage class specifier). Therefore, external communication.
  • c considered in part 3 because it has a static storage class specifier - it has an internal connection.
  • finally, d considered part 6, being a functional parameter - it has no connection.
+6
source

In C, a and b are the external connection, and c is the internal connection. In C ++, you are correct that b will also have an internal relationship.

In the case of "D", I'm not sure what you are talking about: a function or a parameter. func has an external connection; d has no connection.

+4
source

I am sure that by default it has an external connection, b , c and d have an internal binding. Do I understand correctly?

No! d has no relationship, since it is a formal parameter.

From C99

The following identifiers have no binding : 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.

In addition, it has an external connection on the C99. In C ++, const variables in a file region have an internal binding.

+3
source

a, b and c all have an identical relationship if they are declared in the global scope, which look like

d is a parameter of the function and is created when this function is called and no longer exists when the function returns

-2
source

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


All Articles