在一条语句中多次职位和pre增量输出 [英] Output of multiple post and pre increments in one statement
问题描述
我是新的C语言,所以PLZ SUM1帮助我。
编写过C code
I'm new to C language so plz sum1 help me out. A C code written
int i=3;
printf("%d",++i + ++i);
编者GVS O / P = 9。怎么样?结果
感谢名单提前
Complier gvs O/P =9. How?
Thanx in advance
推荐答案
结果是不确定的。您正在修改一个变量不止一次在离pression(或顺序点以更准确)。
The results are undefined. You're modifying a variable more than once in an expression (or sequence point to be more accurate).
修改变量不止一次序列点之间是不确定的,所以不要这么做。
Modifying a variable more than once between sequence points is undefined, so don't do it.
这可能是你的编译器,对于这种特殊情况下决定evalate ++ 1 + + + I
为
It might be your compiler, for this particular case decides to evalate
++i + ++i
as
- 递增最后
++我
,产生4,留我是4 - 递增首
++我
,产生5,更让我为5(如在现有步走了,我为4,它递增5) - 之和两个值,4 + 5。
- increment the last
++i
, yielding 4, leaving i to be 4 - increment the first
++i
, yielding 5, leaving i to be 5 (as the prior step left i as 4, incrementing it to 5) - sum the two values, 4 + 5.
另外一个编译器,或者如果你改变优化级别,或者如果你改变了code略有下降,可能会产生不同的结果。
Another compiler, or if you alter the optimization level, or if you change the code slightly, might produce different output.
这篇关于在一条语句中多次职位和pre增量输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!