问题
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