(++i)+(++i) 的问题.
wxfj
(++i)+(++i) 的问题.
在VC6.0下
#include<stdio.h>
int main()
{
int i=2;
int m;
m = (++i)+(++i);
printf("m=%d/n", m);
return 0;
}
为什么输出
m=8
wxfj
应该 m=7 吧,怎么是8呢? :shock: :shock: :shock:
yecheng_110
编译的时候加上
-Wsequence-point
会有警告
man gcc
看看
-Wsequence-point
77h2_eleven
这个,个人意见
从 4 到 8 都有可能。
Godbach
这个和编译器实现有关。知道这个就可以了。这样的题不用知道结果。
wxfj
[quote]原帖由 [i]77h2_eleven[/i] 于 2008-6-20 10:38 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8625306&ptid=1165252][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
这个,个人意见
从 4 到 8 都有可能。 [/quote]
不可能是4吧?
crspo
gcc -v
[table=95%][tr][td][font=FixedSys]Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --with-tune=i686 --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
[/font][/td][/tr][/table]
gcc -fdump-tree-gimple test.c
cat t.c.t03.gimple
[table=95%][tr][td][font=FixedSys]main ()
{
int D.1770;
int i;
int m;
i = 2;
i = i + 1;
i = i + 1;
m = i + i;
printf (&"m=%d/n"[0], m);
D.1770 = 0;
return D.1770;
}[/font][/td][/tr][/table]
yecheng_110
[quote]原帖由 [i]cjaizss[/i] 于 2008-6-20 11:08 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8625693&ptid=1165252][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
每月都有这么几贴 [/quote]
每个人都是慢慢成长的
Kevin_zqw
写这种东西的人应该枪毙
你想知道为什么是8,去看编译后的汇编代码吧
wxfj
[quote]原帖由 [i]Kevin_zqw[/i] 于 2008-6-20 12:16 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8626449&ptid=1165252][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
写这种东西的人应该枪毙
你想知道为什么是8,去看编译后的汇编代码吧 [/quote]
1: #include<stdio.h>
2:
3: int main()
4: {
0040D6F0 push ebp
0040D6F1 mov ebp,esp
0040D6F3 sub esp,48h
0040D6F6 push ebx
0040D6F7 push esi
0040D6F8 push edi
0040D6F9 lea edi,[ebp-48h]
0040D6FC mov ecx,12h
0040D701 mov eax,0CCCCCCCCh
0040D706 rep stos dword ptr [edi]
5: int i=2;
0040D708 mov dword ptr [ebp-4],2
6: int m;
[color=Red]7: m = (++i)+(++i);
0040D70F mov eax,dword ptr [ebp-4]
0040D712 add eax,1
0040D715 mov dword ptr [ebp-4],eax
0040D718 mov ecx,dword ptr [ebp-4]
0040D71B add ecx,1
0040D71E mov dword ptr [ebp-4],ecx
0040D721 mov edx,dword ptr [ebp-4]
0040D724 add edx,dword ptr [ebp-4]
0040D727 mov dword ptr [ebp-8],edx[/color]
8: printf("m=%d/n", m);
0040D72A mov eax,dword ptr [ebp-8]
0040D72D push eax
0040D72E push offset string "Hello, world!/n" (0042201c)
0040D733 call printf (00401060)
0040D738 add esp,8
9: return 0;
0040D73B xor eax,eax
10: }
果然是编译器的问题!
ldy2534
[quote]原帖由 [i]cjaizss[/i] 于 2008-6-20 11:08 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8625693&ptid=1165252][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
每月都有这么几贴 [/quote]
哈哈。膜拜一下。:luya: :luya: :luya: :luya:
獨照づ幔紗
不一样的编译器 出来的结果也不一样
CSDN经常有这样的帖子
你可以去看看
figure_hit
回复 #1 wxfj 的帖子
像++这种操作,编译器肯定是直接取地址进行引用。