我在某处读到整数常量表达式包含整数常量,例如:
(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/