Multiple increment operators in single statement [duplicate]

微笑、不失礼 提交于 2019-11-28 10:44:15

问题


Possible Duplicate:
Undefined Behavior and Sequence Points

Pleae explain the behaviour of following statements

int b=3;
cout<<b++*++b<<endl;

How will it be calculated?


回答1:


The behavior here is undefined. See this question

Relevant standard quote:

§5/4.1 Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.

The most common sequence point is the end of a statement.

Also worth noting from the standard:

§5.2.2/8 The order of evaluation of arguments is unspecified.




回答2:


The standard says this is undefined. The compiler is free to evaluate the statements in any order is sees fit as long as it follows the operator precedence rules. This results in UB:

b++ * ++b; // b is modified more than once



回答3:


The behavior will be undefined as told by others. The output depends upon the implementation of compiler.

But as per the standard it should be undefined.




回答4:


AS this is undefined behaviour, one can't tell the end result. The result depends on the implementation.




回答5:


Undefined behavior, Compiler is free to evaluate this expression in any order because of the same precedence of the operators. Consider using

(b++)*(++b)

instead



来源:https://stackoverflow.com/questions/6915963/multiple-increment-operators-in-single-statement

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