Multiple increment operators in single statement

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?


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.


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

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.

链接地址: http://www.djcxy.com/p/73242.html

上一篇: 增量如何工作?

下一篇: 单个语句中的多个增量运算符