int i =10; cout << " " << i++<< " " << ++i << " " << i++ << " " << ++i << endl;

real_guang 2005-04-28 10:12:18
int i =10;
cout << " " << i++<< " " << ++i << " " << i++ << " " << ++i << endl;

我输出结果是: 13 13 11 11
谁能解释一下
...全文
183 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
luolovegui 2005-04-28
  • 打赏
  • 举报
回复
另外一句,写程序为什么要写这样的代码,太没意思了。。
luolovegui 2005-04-28
  • 打赏
  • 举报
回复
楼主的结果原因很简单,输出是从右到左运行(计算)的。在C语言里也是这样。。不信你可以用 printf()试试。
柯本 2005-04-28
  • 打赏
  • 举报
回复
如果不优化,结果为
13 13 11 11
汇编如下:
_main PROC NEAR
; File t.cpp
; Line 5
push ebp
mov ebp, esp
sub esp, 12 ; 0000000cH
; Line 6
mov DWORD PTR _i$[ebp], 10 ; 0000000aH
; Line 7
push OFFSET FLAT:?endl@@YAAAVostream@@AAV1@@Z ; endl
mov eax, DWORD PTR _i$[ebp]
add eax, 1
mov DWORD PTR _i$[ebp], eax
mov ecx, DWORD PTR _i$[ebp]
push ecx
push OFFSET FLAT:$SG1342
mov edx, DWORD PTR _i$[ebp]
mov DWORD PTR -8+[ebp], edx
mov eax, DWORD PTR -8+[ebp]
push eax
mov ecx, DWORD PTR _i$[ebp]
add ecx, 1
mov DWORD PTR _i$[ebp], ecx
push OFFSET FLAT:$SG1341
mov edx, DWORD PTR _i$[ebp]
add edx, 1
mov DWORD PTR _i$[ebp], edx
mov eax, DWORD PTR _i$[ebp]
push eax
push OFFSET FLAT:$SG1340
mov ecx, DWORD PTR _i$[ebp]
mov DWORD PTR -12+[ebp], ecx
mov edx, DWORD PTR -12+[ebp]
push edx
mov eax, DWORD PTR _i$[ebp]
add eax, 1
mov DWORD PTR _i$[ebp], eax
push OFFSET FLAT:$SG1339
mov ecx, OFFSET FLAT:?cout@@3Vostream_withassign@@A
call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@P6AAAV0@AAV0@@Z@Z ; ostream::operator<<
; Line 8
mov esp, ebp
pop ebp
ret 0
_main ENDP
_TEXT ENDS
; COMDAT ??6ostream@@QAEAAV0@P6AAAV0@AAV0@@Z@Z
_TEXT SEGMENT
---------------------------------------------------------------------------------
优化编译,结果为
13 14 11 14
汇编如下:
_main PROC NEAR ; COMDAT
; File t.cpp
; Line 7
mov eax, OFFSET FLAT:??_C@_02IHLC@?5?5?$AA@ ; `string'
push 14 ; 0000000eH
push eax
push 11 ; 0000000bH
push eax
mov eax, OFFSET FLAT:??_C@_01FCOA@?5?$AA@ ; `string'
push 14 ; 0000000eH
push eax
push 13 ; 0000000dH
push eax
mov ecx, OFFSET FLAT:?cout@@3Vostream_withassign@@A
call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
push OFFSET FLAT:?flush@@YAAAVostream@@AAV1@@Z ; flush
push 10 ; 0000000aH
mov ecx, eax
call ??6ostream@@QAEAAV0@E@Z ; ostream::operator<<
mov ecx, eax
call ??6ostream@@QAEAAV0@P6AAAV0@AAV0@@Z@Z ; ostream::operator<<
; Line 8
ret 0
_main ENDP
_TEXT ENDS
--------------------------------------------------------------------------------------
这种问题在C/C++版很多,有时与编译器有关,所以在编程时应尽量避免
vcmute 2005-04-28
  • 打赏
  • 举报
回复
The result not only depend on the compiler ,but optimization.
Man_X 2005-04-28
  • 打赏
  • 举报
回复
The result only depend on the compiler.
Man_X 2005-04-28
  • 打赏
  • 举报
回复
from left to right:
1、i++; // i is 10, print "10", then i=i+1, i is 11;
2、++i; // i is 11, i=i+1, i is 12, then print"12";
3、i++; // i is 12, print "12", then i=i+1, i is "13"
4、++i; // i is 13, i=i+1, i is "14", then print"14"

//complied by GCC
vcmute 2005-04-28
  • 打赏
  • 举报
回复
我这是 13 14 11 14
handwolf 2005-04-28
  • 打赏
  • 举报
回复
优先顺序是这样的:"++i" > "<<" > "i++"
所以执行顺序如下:
从右到左,但是"i++"要在"<<"结束以后才进行+1操作

(1) i = i + 1;
(2) <<i;
(3) <<i;
(4) i = i+1;
(5) <<i;
(6) <<i;
(7) i = i+1;
(8) i = i+1;
dfyang 2005-04-28
  • 打赏
  • 举报
回复
from right to left:
1、++i i is 11 and output is 11
2、i++ output i = 11 then i++ ,i = 12
3、++i i is 13 and output is 13
4、i++ output i = 13 then i++ ,i = 14
薛定谔之死猫 2005-04-28
  • 打赏
  • 举报
回复
是人一般不会这么写,不要钻牛角尖,很感兴趣可以分析汇编代码,MS和Borland的编译器对这个问题的处理有明显区别
real_guang 2005-04-28
  • 打赏
  • 举报
回复
不是要写着莫乱,看到一道题,然後,自己写了一下,发现答案不对,解释不清了

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧