gpt4 book ai didi

c - C : does GCC follow the C99 spec, 中的链接还是我不了解规范?

转载 作者:太空狗 更新时间:2023-10-29 17:15:46 25 4
gpt4 key购买 nike

我试图了解 C99 中存储类说明符的确切行为,但某些 GCC 行为似乎不符合规范,除非我误解了规范。来自 6.2.2 (2):

Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function.

但是,我使用以下程序测试了 GCC (powerpc-apple-darwin9-gcc-4.2.1):

#include <stdio.h>
static int f() {
static int x = 0;
return x++;
}
static int g() {
static int x = 0;
return x++;
}
int main(int argc, char *argv[]) {
printf("g() = %i\n", g());
printf("g() = %i\n", g());
printf("f() = %i\n", f());
printf("f() = %i\n", f());
return 0;
}

使用-std=c99编译,打印如下:

g() = 0
g() = 1
f() = 0
f() = 1

如果我正确理解规范,它应该打印:

g() = 0
g() = 1
f() = 2
f() = 3

我理解为什么 GCC 会偏离此处的规范,我只是想知道是否对这种行为有更深入的解释。

最佳答案

6.2.2(6)中说:

The following identifiers have no linkage: [...] a block scope identifier for an object declared without the storage-class specifier extern.

静态变量是对象的 block 范围标识符,它们没有被声明为extern。因此它们没有联系,特别是没有内部联系。

关于c - C : does GCC follow the C99 spec, 中的链接还是我不了解规范?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2159627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com