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

标签 c function static linkage

编译如下:

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/

相关文章:

函数定义中与 double 冲突的类型

java - 复制静态列表的建议

c# - 为什么运算符方法在 C# 中应该是静态的?

C - 使用文件处理和链表登录

r - R 中的参数在函数外部有效但在函数内部失败

java - 关于内存泄漏和泛型方法

c - Switch 语句始终包含 case 和 default

c - float常量隐式转换为int类型什么时候会溢出

c - 以下代码有什么问题?我收到段错误

python - TypeError : NoneType, 相等则可以比较,大于则报错