【C语言】关于i++和++i的又一个话题
ACSolo  发布于 11 年前 1.9k 0 0

#include<stdio.h>
main()
{
int count=0;
printf("%d %d %d\n",count++,count++,count++);
printf("%d %d %d\n",++count,++count,++count);
return 0;
}

/*
0 0 0
6 5 4
*/

 

为啥?请看Disassembly

 

4:        int count=0;
00401028   mov         dword ptr [ebp-4],0
5:        printf("%d %d %d\n",count++,count++,count++);
0040102F   mov         eax,dword ptr [ebp-4]
00401032   mov         dword ptr [ebp-8],eax
00401035   mov         ecx,dword ptr [ebp-8]
00401038   push        ecx
00401039   mov         edx,dword ptr [ebp-4]
0040103C   mov         dword ptr [ebp-0Ch],edx
0040103F   mov         eax,dword ptr [ebp-0Ch]
00401042   push        eax
00401043   mov         ecx,dword ptr [ebp-4]
00401046   mov         dword ptr [ebp-10h],ecx
00401049   mov         edx,dword ptr [ebp-10h]
0040104C   push        edx
0040104D   push        offset string "%d %d %d\n" (0042001c)
00401052   mov         eax,dword ptr [ebp-4]
00401055   add         eax,1
00401058   mov         dword ptr [ebp-4],eax
0040105B   mov         ecx,dword ptr [ebp-4]
0040105E   add         ecx,1
00401061   mov         dword ptr [ebp-4],ecx
00401064   mov         edx,dword ptr [ebp-4]
00401067   add         edx,1
0040106A   mov         dword ptr [ebp-4],edx
0040106D   call        printf (004010f0)
00401072   add         esp,10h
6:        printf("%d %d %d\n",++count,++count,++count);
00401075   mov         eax,dword ptr [ebp-4]
00401078   add         eax,1
0040107B   mov         dword ptr [ebp-4],eax
0040107E   mov         ecx,dword ptr [ebp-4]
00401081   push        ecx
00401082   mov         edx,dword ptr [ebp-4]
00401085   add         edx,1
00401088   mov         dword ptr [ebp-4],edx
0040108B   mov         eax,dword ptr [ebp-4]
0040108E   push        eax
0040108F   mov         ecx,dword ptr [ebp-4]
00401092   add         ecx,1
00401095   mov         dword ptr [ebp-4],ecx
00401098   mov         edx,dword ptr [ebp-4]
0040109B   push        edx
0040109C   push        offset string "%d %d %d\n" (0042001c)
004010A1   call        printf (004010f0)
004010A6   add         esp,10h
7:        return 0;