Clarification on integer constant expressions

谁说胖子不能爱 提交于 2019-12-01 05:35:55

问题


Somewhere I've read that integer constant expressions consists integer constants such as:

(5 + 5) //integer constant expression

That was the only example I have seen.

Now, from standard which says:

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

So, are all these integer expressions below if I'm right?

int i; float f = 3.14f;

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

回答1:


(int)(5/3.14f) is not an integer constant expression.

You are using a floating constant that is not an immediate operands of cast.

And of course in i = f; the object f is not an integer constant expression.



来源:https://stackoverflow.com/questions/14081737/clarification-on-integer-constant-expressions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!