|
发表于 2013-10-17 08:17:39
|
显示全部楼层
这种问题不用太纠结,一般都不会写这种代码的,由于编译器的不同,结果也不同。
经反汇编可以看出,第一题 i 先自增两次之后变成 3 ,然后 3 + 3 = 68: printf (" %d\n ",(++i)+(++i));
0040102F mov eax,dword ptr [ebp-4]
00401032 add eax,1
00401035 mov dword ptr [ebp-4],eax
00401038 mov ecx,dword ptr [ebp-4]
0040103B add ecx,1
0040103E mov dword ptr [ebp-4],ecx
00401041 mov edx,dword ptr [ebp-4]
00401044 add edx,dword ptr [ebp-4]
00401047 push edx
00401048 push offset string " %d\n " (0042201c)
0040104D call printf (004010a0)
00401052 add esp,8
第二题
p = 1 + 1 +1 =3 i =4
q = 1 + 1 + 1 =3 j = 4
开始 i = 4 然后 i自增一次为6 接着6+6=12 接着i自增一次为7 7+12=19 m = 6 + 6 + 7 = 19
开始j = 4 然后 j 自增一次为5 接着 j 自增一次为6 6+6=12 接着 j 自增一次为7 7 + 12 =19
n = 6 + 6 + 7 =196: int i = 1, j = 1, p, q, m, n;
0040D748 mov dword ptr [ebp-4],1
0040D74F mov dword ptr [ebp-8],1
7:
8: p = ( i++ ) + ( i++ ) + ( i++ );
0040D756 mov eax,dword ptr [ebp-4]
0040D759 add eax,dword ptr [ebp-4]
0040D75C add eax,dword ptr [ebp-4]
0040D75F mov dword ptr [ebp-0Ch],eax
0040D762 mov ecx,dword ptr [ebp-4]
0040D765 add ecx,1
0040D768 mov dword ptr [ebp-4],ecx
0040D76B mov edx,dword ptr [ebp-4]
0040D76E add edx,1
0040D771 mov dword ptr [ebp-4],edx
0040D774 mov eax,dword ptr [ebp-4]
0040D777 add eax,1
0040D77A mov dword ptr [ebp-4],eax
9: q = j++ + j++ + j++ ;
0040D77D mov ecx,dword ptr [ebp-8]
0040D780 add ecx,dword ptr [ebp-8]
0040D783 add ecx,dword ptr [ebp-8]
0040D786 mov dword ptr [ebp-10h],ecx
0040D789 mov edx,dword ptr [ebp-8]
0040D78C add edx,1
0040D78F mov dword ptr [ebp-8],edx
0040D792 mov eax,dword ptr [ebp-8]
0040D795 add eax,1
0040D798 mov dword ptr [ebp-8],eax
0040D79B mov ecx,dword ptr [ebp-8]
0040D79E add ecx,1
0040D7A1 mov dword ptr [ebp-8],ecx
10: m = ( ++i ) + ( ++i ) + ( ++i );
0040D7A4 mov edx,dword ptr [ebp-4]
0040D7A7 add edx,1
0040D7AA mov dword ptr [ebp-4],edx
0040D7AD mov eax,dword ptr [ebp-4]
0040D7B0 add eax,1
0040D7B3 mov dword ptr [ebp-4],eax
0040D7B6 mov ecx,dword ptr [ebp-4]
0040D7B9 add ecx,dword ptr [ebp-4]
0040D7BC mov edx,dword ptr [ebp-4]
0040D7BF add edx,1
0040D7C2 mov dword ptr [ebp-4],edx
0040D7C5 add ecx,dword ptr [ebp-4]
0040D7C8 mov dword ptr [ebp-14h],ecx
11: n = ++j + ++j + ++j ;
0040D7CB mov eax,dword ptr [ebp-8]
0040D7CE add eax,1
0040D7D1 mov dword ptr [ebp-8],eax
0040D7D4 mov ecx,dword ptr [ebp-8]
0040D7D7 add ecx,1
0040D7DA mov dword ptr [ebp-8],ecx
0040D7DD mov edx,dword ptr [ebp-8]
0040D7E0 add edx,dword ptr [ebp-8]
0040D7E3 mov eax,dword ptr [ebp-8]
0040D7E6 add eax,1
0040D7E9 mov dword ptr [ebp-8],eax
0040D7EC add edx,dword ptr [ebp-8]
0040D7EF mov dword ptr [ebp-18h],edx
|
|