求助两段代码为什么输出结果不一样?

kuillldan 2009-11-12 09:35:56
劳驾各位帮忙看看下面两段代码输出结果为什么不一样啊?
代码一:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


char *getname();
void main()
{
char a[20];
cin>>a;
int i = 0;
while(a[i] != 0)
{
cout<<a[i]<<":"<<(int)a[i++]<<endl;//mark
}
system("pause");
}


代码二:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


char *getname();
void main()
{
char a[20];
cin>>a;
int i = 0;
while(a[i] != 0)
{
cout<<a[i]<<":"<<(int)a[i]<<endl;
i++;//mark
}
system("pause");
}
...全文
141 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
AbnormalSubmarine 2009-11-13
  • 打赏
  • 举报
回复

20: char a[20];
21: cin>>a;
004010C8 lea eax,[ebp-14h] ;ebp-14保存a的地址0x0012ff6c
004010CB push eax
004010CC mov ecx,offset cin (0042aa68) ;
004010D1 call istream::operator>> (00402740) ;"aaaaaaaaaaaaaaaa"
22: int i = 0;
004010D6 mov dword ptr [ebp-18h],0 ;0x0012ff68=0
23: while(a[i] != 0)
004010DD mov ecx,dword ptr [ebp-18h] ;cx=0
004010E0 movsx edx,byte ptr [ebp+ecx-14h] ;字符a的ASCI码值=0x61
004010E5 test edx,edx
004010E7 je main+84h (00401134)
24: {
25: cout<<a[i]<<":"<<(int)a[i++]<<endl;
004010E9 push offset @ILT+10(endl) (0040100f) ;endl函数的偏移入栈
004010EE mov eax,dword ptr [ebp-18h] ;ebp-18h=变量i
004010F1 movsx ecx,byte ptr [ebp+eax-14h] ;cx存储数组元素内容
004010F6 mov dword ptr [ebp-1Ch],ecx
004010F9 mov edx,dword ptr [ebp-1Ch] ;把数组中元素转存edx
004010FC push edx
004010FD mov eax,dword ptr [ebp-18h] ;i++
00401100 add eax,1
00401103 mov dword ptr [ebp-18h],eax
00401106 push offset string ":" (0042701c) ;偏移指针到":"
0040110B mov ecx,dword ptr [ebp-18h] ;把自增后的i值存入cx
0040110E mov dl,byte ptr [ebp+ecx-14h] ;数组中下一个元素的内容存入dl
00401112 push edx
00401113 mov ecx,offset cout (0042aa28) ;ecx保存cout函数的偏移地址
00401118 call @ILT+5(ostream::operator<<) (0040100a)
0040111D mov ecx,eax
0040111F call ostream::operator<< (004015f0)
00401124 mov ecx,eax
00401126 call ostream::operator<< (004012b0)
0040112B mov ecx,eax
0040112D call @ILT+0(ostream::operator<<) (00401005)
26: }



分析了一部分,另外一部分自己分析
zgjxwl 2009-11-12
  • 打赏
  • 举报
回复
while(a[i] != 0)
{
cout<<a[i]<<":"<<(int)a[i++]<<endl;//mark
}


标准规定,求值顺序未定义,具体依赖于编译器。。。
C++ PRIMER也提到,不要在同一条语句中多次修改同一个变量的值,因为那是未定义的。
所以。。。你要是有疑问,写成两条语句就行了。。。
while(a[i] != 0)
{
cout<<a[i]<<":"<<endl;
cout<<(int)a[i++]<<endl;//mark
}
xladykiller 2009-11-12
  • 打赏
  • 举报
回复
不一样是真常的,a[20]没有初始化,,里面的值是不确定的,,所以两个程序运行结果不一样,,,就是同一个程序运行两 次,结果也会不一样的。
AbnormalSubmarine 2009-11-12
  • 打赏
  • 举报
回复

20: char a[20];
21: cin>>a;
004010C8 lea eax,[ebp-14h]
004010CB push eax
004010CC mov ecx,offset cin (0042aa68)
004010D1 call istream::operator>> (00402740)
22: int i = 0;
004010D6 mov dword ptr [ebp-18h],0
23: while(a[i] != 0)
004010DD mov ecx,dword ptr [ebp-18h]
004010E0 movsx edx,byte ptr [ebp+ecx-14h]
004010E5 test edx,edx
004010E7 je main+84h (00401134)
24: {
25: cout<<a[i]<<":"<<(int)a[i++]<<endl;//mark
004010E9 push offset @ILT+10(endl) (0040100f)
004010EE mov eax,dword ptr [ebp-18h]
004010F1 movsx ecx,byte ptr [ebp+eax-14h]
004010F6 mov dword ptr [ebp-1Ch],ecx
004010F9 mov edx,dword ptr [ebp-1Ch]
004010FC push edx
004010FD mov eax,dword ptr [ebp-18h]
00401100 add eax,1
00401103 mov dword ptr [ebp-18h],eax
00401106 push offset string ":" (0042701c)
0040110B mov ecx,dword ptr [ebp-18h]
0040110E mov dl,byte ptr [ebp+ecx-14h]
00401112 push edx
00401113 mov ecx,offset cout (0042aa28)
00401118 call @ILT+5(ostream::operator<<) (0040100a)
0040111D mov ecx,eax
0040111F call ostream::operator<< (004015f0)
00401124 mov ecx,eax
00401126 call ostream::operator<< (004012b0)
0040112B mov ecx,eax
0040112D call @ILT+0(ostream::operator<<) (00401005)
26: }




20: char a[20];
21: cin>>a;
004010C8 lea eax,[ebp-14h]
004010CB push eax
004010CC mov ecx,offset cin (0042aa68)
004010D1 call istream::operator>> (00402740)
22: int i = 0;
004010D6 mov dword ptr [ebp-18h],0
23: while(a[i] != 0)
004010DD mov ecx,dword ptr [ebp-18h]
004010E0 movsx edx,byte ptr [ebp+ecx-14h]
004010E5 test edx,edx
004010E7 je main+7Eh (0040112e)
24: {
25: cout<<a[i]<<":"<<(int)a[i]<<endl;
004010E9 push offset @ILT+10(endl) (0040100f)
004010EE mov eax,dword ptr [ebp-18h]
004010F1 movsx ecx,byte ptr [ebp+eax-14h]
004010F6 push ecx
004010F7 push offset string ":" (0042701c)
004010FC mov edx,dword ptr [ebp-18h]
004010FF mov al,byte ptr [ebp+edx-14h]
00401103 push eax
00401104 mov ecx,offset cout (0042aa28)
00401109 call @ILT+5(ostream::operator<<) (0040100a)
0040110E mov ecx,eax
00401110 call ostream::operator<< (004015f0)
00401115 mov ecx,eax
00401117 call ostream::operator<< (004012b0)
0040111C mov ecx,eax
0040111E call @ILT+0(ostream::operator<<) (00401005)
26: i++;
00401123 mov ecx,dword ptr [ebp-18h]
00401126 add ecx,1
00401129 mov dword ptr [ebp-18h],ecx
27: }



VC6.0
自己比较哈
xiaoye 2009-11-12
  • 打赏
  • 举报
回复
给你写个简单点的程序,
#include <iostream>
using namespace std;
int main(int argc ,char *argv[]){
int a=0;
cout << a << a++ << endl;
return 0;
}
输出结果: 10
<< 运算符虽然是左结合性的,但对于它所输出的变量是从右面开始计算的),这和c语言中的printf()类似,
即先执行a++,之后才算a的值.
kouwenlong 2009-11-12
  • 打赏
  • 举报
回复
cout<<a[i]<<":"<<(int)a[i++]<<endl;//mark
分解为:
临时变量temp = (int)a[i];
++i;
cout<<a[i]<<temp<<endl;

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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