Multiple increment operators in single statement [duplicate]

情到浓时终转凉″ 提交于 2019-11-29 16:27:28
Greg Howell

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.

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

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

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