具有块作用域的标识符的声明何时可以具有内部链接?

When can a declaration of an identifier that has block scope have internal linkage?

我正在围绕 'C' 标准转变,我遇到了这个:

$6.7.9.5:

If the declaration of an identifier has block scope, and the identifier has external or internal linkage, the declaration shall have no initializer for the identifier.

所以我的问题就在标题上。如果可能的话,我也想要一些例子。

static int i; // internal linkage

void f() {
   extern int i; // block-scope declaration; refers to i in global scope
                 // still internal linkage - see 6.2.2/4
}

意思如下

#include <stdio.h>

static int x = 1;

int main()
{
    int y = 2;

    {
        extern int x;

        printf( "x + y = %d\n", x + y );
    }
}

此处 inside main in internal 块变量 x 具有内部链接并表示在 main 外部定义的相同变量 x

但是在这个程序中

#include <stdio.h>

static int x = 1;

int main()
{
    int y = 2;
    int x = 3;

    {
        extern int x;

        printf( "x + y = %d\n", x + y );
    }
}

用说明符 extern 声明的变量 x 具有外部链接,它与定义在 main 之前的变量 x 不同。原因是其他局部变量x隐藏了main.

之前定义的全局变量x

您应该下载更新版本的 C 标准。查找 C11 标准的最新草案 n1570.pdf。它的语言更明确:

6.2.2 Linkages of identifiers

  1. An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. There are three kinds of linkage: external, internal, and none.

  2. In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

  3. If the declaration of a file scope identifier for an object or a function contains the storage- class specifier static, the identifier has internal linkage.

  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.

  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.

  6. The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.

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