当非静态函数声明跟在静态函数声明之后会发生什么?

What happens when non-static function declaration follows static function declaration?

编译如下:

static int foo() { return 1; }
int foo();

但是,它会一直编译吗?这种情况下的行为是否定义明确?当非静态原型遵循静态声明时,这意味着什么?

是的,它将编译并且行为定义明确。由于 foo 声明 static 早于 int foo();1foo 具有内部链接.

C11:6.2.2 标识符的链接(p4):

For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible,31) 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. [...]

脚注指出:

31) As specified in 6.2.1, the later declaration might hide the prior declaration.


1。如果未指定存储 class,则假定该函数具有外部链接。标准说:如果一个函数的标识符声明没有存储-class说明符,它的链接 就像用存储-class 说明符 extern 声明一样确定 -- 6.2.2 (p5).

默认情况下函数是全局的。所以制作

static int foo() { return 1; }

函数 foo() 仅在此文件中可见。由于您只有声明 int foo(); 这很好并且定义明确,如果您有相同的 int foo(){ return 2;} 的定义那么您将得到 重新定义错误 .

@haccks所述

6.2.1, the later declaration might hide the prior declaration.

注意声明和定义的区别。