Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we declare a file scope identifier with internal linkage without the static keyword?

Tags:

c

Section 6.11.2 of the C standard states one of the features that may be deprecated in future versions:

Declaring an identifier with internal linkage at file scope without the static storage-class specifier is an obsolescent feature.

How is it even possible to declare a file scope identifier to have internal linkage without the static storage-class specifier?

like image 603
cmutex Avatar asked Apr 30 '20 18:04

cmutex


People also ask

Which keyword is used for internal linkage?

To use internal linkage we have to use which keyword? Explanation: static keyword is used for internal linkage.

Is static internal linkage?

The scope of a global variable can be restricted to the file containing its declaration by prefixing the declaration with the keyword static . Such variables are said to have internal linkage.

What is internal and external linkage in C?

Both are decided by linkage. Linkage thus allows you to couple names together on a per file basis, scope determines visibility of those names. There are 2 types of linkage: Internal Linkage: An identifier implementing internal linkage is not accessible outside the translation unit it is declared in.

What are the types of linkages 1 point internal and external external internal and none external and none internal?

Explanation: External Linkage-> means global, non-static variables and functions. Internal Linkage-> means static variables and functions with file scope. None Linkage-> means Local variables.


1 Answers

There is a specific example for this issue in the C++ standard. In [dcl.stc]:

static int b; // b has internal linkage
extern int b; // b still has internal linkage

So in the second line, b is declared as a file scope identifier with internal linkage without the keyword static. The example is also true for C, as C11:6.2.2 says:

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.

see this question for more detail

like image 74
knir Avatar answered Oct 01 '22 02:10

knir