问题描述
/*on dev-cpp output=10
visual c++ output=12
why*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
i=1;
j=(++i)+(++i)+(++i);
printf("%d",j);
getch();
}
[edit]已添加代码块,HTML编码 - OriginalGriff [/ edit]
[edit]Code block added, HTML encoded - OriginalGriff[/edit]
推荐答案
首先,使用 - 这意味着增量操作应该在使用 i 的值之前进行code>。但是,之前有多少?
First, you use the Prefix Increment Operator - which means that the increment operation should occur before the use of the value of i
. But, how much before?
似乎第一个编译器在使用它们之前直接递增这些值。因此,由于我们有两个 +
运算符,前两个增量在第一个 +
运算符和第三个增量之前执行在第二个 +
运算符之前执行。给我们的是什么: 3 + 3 + 4 = 10
。
It seems like the first compiler increments the values directly before they are used. So, as we have two +
operators, the first two increments are performed before the first +
operator and the third increment is performed before the second +
operator. What gives us: 3 + 3 + 4 = 10
.
第二个编译器执行整个增量声明的开头。是什么给了我们: 4 + 4 + 4 = 12
。
The second compiler performs the whole of the increments at the beginning of the statement. What gives us: 4 + 4 + 4 = 12
.
2 + 3 + 4 = 9
不是12或10!
not 12 or 10!
这篇关于由编译器引起的ambiguty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!