鱼C论坛

 找回密码
 立即注册
查看: 1477|回复: 5

[已解决]为什么这个自增自减运算的输出结果和我算的不一样呢?

[复制链接]
发表于 2019-9-10 23:14:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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
最佳答案
2019-9-11 10:07:41
本帖最后由 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-9-10 23:28:59 | 显示全部楼层
这类问题没有正确答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-11 05:51:07 | 显示全部楼层
楼上小男神说的么错,这个编译器执行问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-11 06:43:17 From FishC Mobile | 显示全部楼层
人造人 发表于 2019-9-10 23:28
这类问题没有正确答案

第二个8不太能理解
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-11 10:07:41 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-11 12:52:57 | 显示全部楼层
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
我好像有点明白了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-4 09:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表