整型常量表达式的说明

标签 c c99

我在某处读到整数常量表达式包含整数常量,例如:

(5 + 5) //integer constant expression

这是我见过的唯一例子。

现在,从标准说:

(C99 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, 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.

那么,如果我是对的,下面所有这些整数表达式都是吗?

int i; float f = 3.14f;

i = 42;
i = f;
i = (int)(5/3.14f);

最佳答案

(int)(5/3.14f) 不是整数常量表达式。

您使用的浮点常量不是强制转换的立即操作数

当然,在 i = f; 中,对象 f 不是整数常量表达式。

关于整型常量表达式的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14081737/

相关文章:

c - 如何声明函数原型(prototype)及其参数声明列表?

C 错误 : "initializer element is not constant" with &, 适用于 +

c++ - 在Visual Studio中查找非法的内存访问

c - 如何调试使用 gdb 中辅助 txt 文件输入的程序?

c - 为什么 C99 中不包含定点类型?

通过相应标准 I/O 流的输入/输出是否会失败?

c - 是否有任何现有的 C 实现在(无)符号整数表示中具有填充位?

c - 如何将一个进程的标准输出重定向到另一个进程的标准输入?

c - 为什么这个 OpenMP 程序每次都给我不同的答案?

c - 错误: Invalid use of flexible array member