c - 涉及 float 的 autoconf 检查的非常量存储大小?

标签 c gcc floating-point autotools autoconf

我正在处理一些 autoconf 的东西,并且有一个测试包括以下内容:

static int test_array [1 - 2 * !((((float)((int)((float)1.4))) == ((float)1.4)) >= 0)];

这失败了:

error: storage size of ‘test_array’ isn’t constant

但是当我将其更改为:

static int test_array [1 - 2 * !((((int)((int)((int)1.4))) == ((int)1.4)) >= 0)];

它工作正常。

我不是很熟悉 autotools 使用的所有技巧,但对我来说,这两种方法的值似乎都应该在编译时确定。

为什么第一个失败了?

最佳答案

让我们看看标准(6.6(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, _Alignof expressions, 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 or _Alignof operator.

(强调我的)。

您不能在整数常量表达式中强制转换为float(sizeof_Alignof 的参数除外),因此

1 - 2 * !((((float)((int)((float)1.4))) == ((float)1.4)) >= 0)

不是整数常量表达式。

static 数组的元素数必须是求值为正整数的整数常量表达式。

关于c - 涉及 float 的 autoconf 检查的非常量存储大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200198/

相关文章:

C 从二进制 float

c - float 输出不正确

c - 全局变量始终初始化为零

c - 如何在我的控制台应用程序 (C) 中使用颜色?

python - 如何将二维数组从 Python 传递到 C?

c++ - 在if语句中使用切换用例

c++ - 如何让 gcc 为所有符号名称添加前缀

c - 如何打印具有相同名称的全局变量和局部变量的值?

floating-point - 浮点错误的真实例子

c - 使用 C 中的 read(2) 函数从缓冲区打印