问题描述
我最近了解了,
运营商,它引入了的序列点的事实的
I recently learned about the ,
operator and the fact that it introduces a sequence point.
我还了解到,以下code导致不确定的行为:
I also learned that the following code led to undefined behavior:
i = ++i;
由于 I
在两个序列点之间两次修改
Because i
was modified twice between two sequence points.
但是,我们下面的codeS?
But what about the following codes ?
i = 0, ++i;
i = (0, ++i);
虽然我知道规则,我不能得到一个结论。因此,它是定义的行为或不?
While I know the rules, I can't get to a conclusion. So is it defined behavior or not ?
编辑:正如@paxdiablo提到,的定义与否的,这的确是一个坏习惯应该避免的。这个问题是问仅用于教育目的和更好地理解规则。
edit: Just as @paxdiablo mentions, defined or not, this is really a bad practice which should be avoided. This question is asked solely for educational purposes and better understanding of the "rules".
推荐答案
是的。 =
具有较高的precedence比,
,所以这个前pression等同于(I = 0),++我
。 ,
是一个序列点,所以它保证了 ++我
转让后发生。
Yes. =
has higher precedence than ,
, so this expression is equivalent to (i = 0), ++i
. ,
is a sequence point, so it's guaranteed that the ++i
occurs after the assignment.
我不知道是否 I =(0,+ I)
虽然是定义。我的猜测是没有;还有的增量和分配之间没有顺序点。
I'm not sure whether i = (0, ++i)
is defined though. My guess would be no; there's no sequence point between the increment and the assignment.
这篇关于为i = 0,+我定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!