c - GCC 不支持简单的整数常量表达式?

标签 c gcc c99

海湾合作委员会 4.9 和 5.1 reject这个在全局范围内的简单 C99 声明。 Clang 接受它。

const int a = 1, b = a; // error: initializer element is not constant

这么基本的功能怎么能少呢?看起来很简单。

最佳答案

C991 部分 6.6 常量表达式 是控制部分。它在 67 小节中指出:

6/ An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts.

Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.

整数和浮点常量的定义在标准的 6.4.4 中指定,并且仅限于实际的(文字)而不是变量。

7/ More latitude is permitted for constant expressions in initializers. Such a constant expression shall be, or evaluate to, one of the following (a) an arithmetic constant expression, (b) a null pointer constant, (c) an address constant, or (d) an address constant for an object type plus or minus an integer constant expression.

因为 a 不是 67 小节中的那些东西,所以它不被认为是一个符合标准的常量表达式。

因此,真正的问题不是为什么 gcc 拒绝它,而是为什么 clang 接受它,而这似乎隐藏在 10 小节中> 同一部分的:

10/ An implementation may accept other forms of constant expressions.

换句话说,该标准规定了实现必须允许常量表达式,但不限制实现允许。


1 除了允许 _Alignofsizeof 等小事外,C11 与 C11 基本相同。

关于c - GCC 不支持简单的整数常量表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158413/

相关文章:

c++ - malloc 或我的错误?

qt - 在Qt中更改编译器

C99 不是 GCC 的默认 C 版本?

在C99模式下给出编译错误的代码

c - 用户定义的最小和最大函数的奇怪行为

c++ - C/C++ : sizeof(short), sizeof(int)、sizeof(long)、sizeof(long long) 等...在 32 位机器上与在 64 位机器上

objective-c - "Swift is build on top of C and Objective C without constraints of C compatibility"是什么意思?

python - 在C中将Float转换为String,然后在Python中读取它

gcc - python-ldap 3.4 在 Alpine 图像上安装失败

c - 如何使用但不公开 c99 库中的内联函数?