|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我刚开始学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
本帖最后由 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
|
|