为什么x-> 0`不是未定义的行为,而`x = x--`是? [英] Why is `x-- > 0` not undefined behaviour, while `x = x--` is?
本文介绍了为什么x-> 0`不是未定义的行为,而`x = x--`是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
众所周知,这会循环到零:
As everyone knows, this loops through zero:
while (x-- > 0) { /* also known as x --> 0 */
printf("x = %d\n", x);
}
但是x = x--
会产生不确定的行为.
两个示例都需要x--
的一些返回"值,我猜这是不存在的.
Both examples need some 'return' value of x--
, which is not there I guess. How can it be that x-- > 0
is defined but x = x--
is not?
推荐答案
因为在x = x--
中,您两次修改了x
的值,而没有中间的顺序点.因此,操作顺序未定义.在x-- > 0
中,对x
的值进行了一次修改,并且明确定义了x--
的求值结果将是递减之前的x
的值.
Because in x = x--
you're modifying the value of x
twice without an intervening sequence point. So the order of operations is not defined. In x-- > 0
the value of x
is modified once, and it is clearly defined that result of evaluating x--
will be the value of x
before the decrement.
这篇关于为什么x-> 0`不是未定义的行为,而`x = x--`是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文