差异I = I + + + +的C输出;且i = ++ I + C ++; [英] Difference in output of i=i++ + ++c; and i=++i + c++;
问题描述
我不知道这是编译器特定的,但是当我试图运行两位前pressions DEVC ++
I don't know if this is compiler specific but when I tried running the two expressions in DevC++
在 I = C = B = 0;
I = I + + + +ç
给 2
,而 I = + I + C ++
给 1
不过,
B = I + + + +ç
和
B = + I + + +ç
产生的结果 1
这两个前pressions。
But
b=i++ + ++c
and
b=++i + ++c
produces the result 1
for both expressions.
我不知道,在一个未定义的值在相同的前pression结果递增一个变量两次根据 C 标准规范,但我很好奇如何编译器生成这些输出。可能有人请解释如何,为什么?
I do know that incrementing a variable twice in the same expression results in an undefined value according to the C standard specification but I'm curious how the compiler produces these output. Could someone please explain how and why?
推荐答案
我++
和 ++我
完全不同,我++
是后递增,这意味着评估 I
在EX pression然后递增一次的评估。 ++我
表示则增量评估前pression。
我在你的例子中看到您设置 I = ++ I / I ++
,这是因为在评论中提及未定义的行为。
i++
and ++i
are completely different, i++
is post increment which means evaluate i
in the expression and then increment once its evaluated. ++i
means increment then evaluate the expression.
I see in your example you set i = ++i/i++
, this is undefined behavior as mentioned in a comment.
这篇关于差异I = I + + + +的C输出;且i = ++ I + C ++;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!