编译如下:
static int foo() { return 1; }
int foo();
但是,它总是能编译吗?这种情况下的行为是否明确定义?当非静态原型(prototype)遵循静态声明时意味着什么?
最佳答案
是的,它会编译并且行为被明确定义。由于 foo
早于 int foo();
1 声明为 static
,因此 foo
有内部链接。
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。如果未指定存储类,则假定该函数具有外部链接。标准规定:如果函数的标识符声明没有存储类说明符,则其链接
完全按照存储类说明符 extern
来确定 - 6.2.2 (p5)。
关于c - 当非静态函数声明跟在静态函数声明之后会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59026550/