为什么这个自增自减运算的输出结果和我算的不一样呢?
我刚开始学C,用的是VS2015编译器,刚照着打的代码,但是这个结果把我搞晕了,谁能告诉我正确答案是应该是多少?#include"stdio.h"
void main()
{
int i = 8;
printf("%d\n%d\n%d\n%d\n%d\n%d\n", ++i, --i, i++, i--, -i++, -i--);
}
运行结果:
8
8
7
8
-7
-8 这类问题没有正确答案
楼上小男神说的么错,这个编译器执行问题{:10_256:} 人造人 发表于 2019-9-10 23:28
这类问题没有正确答案
第二个8不太能理解{:10_254:} 本帖最后由 xypmyp 于 2019-9-11 11:49 编辑
#include "stdio.h"
#include "stdlib.h"
int main()
{
int i = 8;
printf("%d\n%d\n%d\n%d\n%d\n%d\n\n", ++i, --i, i++, i--, -i++, -i--);
/*
Problem relate to call standard (cdcall)
Compiler push the value from the right first, it -i--
Step 1:
tempStorageB = (8+1-1) = 8 // Calculate ++i --i first
tempStorageA = i = 8 // For lower priority
step 2:
registerA = -(tempStorageA)
registerA = -8 //Print value
tempStorageA = tempStorageA -1 = 7; //Store value
step 3:
registerB = -(tempStorageA)
registerB = -7
tempStorageA = tempStorageA +1 = 8;
step 4:
registerC = (tempStorageA)
registerC = 8
tempStorageA = tempStorageA -1 = 7;
step 5:
registerD = (tempStorageA)
registerD = 7
tempStorageA = tempStorageA +1 = 8;
step 6:
registerE = (tempStorageB)
registerE = 8
step 7:
registerF = (tempStorageB)
registerF = 8
*/
/*The ans will be 8, 8, 7, 8, -7, -8*/
getchar();
return 0;
}
That's my idea, may be wrong xypmyp 发表于 2019-9-11 10:07
#include "stdio.h"
#include "stdlib.h"
我把代码修改了一下
#include"stdio.h"
void main()
{
int i = 8;
printf("%d\n%d\n%d\n%d\n%d\n%d\n", ++i, --i, --i, ++i, ++i, --i);
}
运行结果显示:
8
8
8
8
8
8
我好像有点明白了
页:
[1]