输出的一个问题

永远的明日 2009-05-02 12:48:36
为什么输出'ABC'是4276803??

#include<iostream>
using namespace std;
int main()
{
cout<<'ABC'<<endl;//输出4276803
return 0;
}
...全文
161 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
deltamaster 2009-05-03
  • 打赏
  • 举报
回复
单引号内多个字符,属于未定义的行为,编译器有权以任何形式解释这种代码。
  • 打赏
  • 举报
回复
'ABC'
这个只是一个字符,你要编译器怎么理解。
Vegertar 2009-05-02
  • 打赏
  • 举报
回复
'ABC' ---->> "ABC"
LitterMonster 2009-05-02
  • 打赏
  • 举报
回复
对了很佩服hw324306893的数字分析能力 'A' * 65536 + 'B' * 256 + 'C' = 4276803 这感觉有点像在猜密码结构

你可以将程序修改成

cout<<'ABCD'<<endl;
得到的结果是: 1094861636
也就是0x41424344的十进制值


如果修改成
cout<<'ABCDE'<<endl;
得到的结果是: 1111704645
转换为十六进制是: 0x42434445
为什么会这样呢? 为什么不是0x4142434445呢?
因为cout将单引号字符常量''当做整数来处理,而且把这个整数是当做32位的值来处理的。所以是0x42434445


windsting 2009-05-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 SmallSmallSmallSmall 的回复:]
这样输出:

C/C++ codecout << hex << 'ABC' << endl;



你就能大概揣摩一下你用的编译器是怎样处理'ABC'这种东西的。
[/Quote]

顶,精彩的回答,

[Quote=引用 11 楼 LitterMonster 的回复:]

'ABC' 在内存中以0x414243存放
它们分别对应ASCII码 0x41 表示'A' 0x42 表示'B' 0x42表示'C'
而这个0x414243值正好等于4276803

因此,cout对用单引号''传入进来的字符常量是将其做为整数来看待。

[/Quote]

顶,认真、细致而严谨的回答。
LitterMonster 2009-05-02
  • 打赏
  • 举报
回复


我通过gdb调试发现如下:

C:\MinGWStudio\Help\Hello\Debug>gdb
GNU gdb 20040521
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-mingw32".
(gdb) file Hello.exe
Reading symbols from C:\MinGWStudio\Help\Hello\Debug/Hello.exe...done.
(gdb) run
Starting program: C:\MinGWStudio\Help\Hello\Debug/Hello.exe
4276803

Program exited normally.
(gdb) list
1 #include <iostream>
2
3 using namespace std;
4 int main()
5 {
6 cout<<'ABC'<<endl;
7 return 0;
8 }(gdb) list
Line number 9 out of range; hello.cpp has 8 lines.
(gdb) break 6
Breakpoint 1 at 0x4013ba: file hello.cpp, line 6.
(gdb) run
Starting program: C:\MinGWStudio\Help\Hello\Debug/Hello.exe

Breakpoint 1, main () at hello.cpp:6
6 cout<<'ABC'<<endl;
(gdb) disassemble 6
No function contains specified address.

(gdb) disassemble
Dump of assembler code for function main:
0x00401390 <main+0>: push %ebp
0x00401391 <main+1>: mov %esp,%ebp
0x00401393 <main+3>: sub $0x18,%esp
0x00401396 <main+6>: and $0xfffffff0,%esp
0x00401399 <main+9>: mov $0x0,%eax
0x0040139e <main+14>: add $0xf,%eax
0x004013a1 <main+17>: add $0xf,%eax
0x004013a4 <main+20>: shr $0x4,%eax
0x004013a7 <main+23>: shl $0x4,%eax
0x004013aa <main+26>: mov %eax,0xfffffffc(%ebp)
0x004013ad <main+29>: mov 0xfffffffc(%ebp),%eax
0x004013b0 <main+32>: call 0x40d080 <_alloca>
0x004013b5 <main+37>: call 0x40ccc0 <__main>
0x004013ba <main+42>: movl $0x414243,0x4(%esp)0x004013c2 <main+50>: movl $0x4433c0,(%esp)
0x004013c9 <main+57>: call 0x42ade0 <_ZNSolsEi>
0x004013ce <main+62>: movl $0x43ae18,0x4(%esp)
0x004013d6 <main+70>: mov %eax,(%esp)
0x004013d9 <main+73>: call 0x429fa0 <_ZNSolsEPFRSoS_E>
0x004013de <main+78>: mov $0x0,%eax
0x004013e3 <main+83>: leave
0x004013e4 <main+84>: ret
---Type <return> to continue, or q <return> to quit---
End of assembler dump.
(gdb)


'ABC' 在内存中以0x414243存放
它们分别对应ASCII码 0x41 表示'A' 0x42 表示'B' 0x42表示'C'
而这个0x414243值正好等于4276803

因此,cout对用单引号''传入进来的字符常量是将其做为整数来看待。
qq25539088 2009-05-02
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
int main()
{
cout<<"ABC"<<endl; //输出ABC
return 0;
}

改成这样就对了
hbueducn 2009-05-02
  • 打赏
  • 举报
回复
为什么是*65536和*256?16位和8位?解释一下
hw324306893 2009-05-02
  • 打赏
  • 举报
回复
额~ 恶心啊 'A' * 65536 + 'B' * 256 + 'C' = 4276803
lpf000 2009-05-02
  • 打赏
  • 举报
回复
当成 一个数处理了?
永远的明日 2009-05-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 SmallSmallSmallSmall 的回复:]
这样输出:

C/C++ codecout << hex << 'ABC' << endl;



你就能大概揣摩一下你用的编译器是怎样处理'ABC'这种东西的。
[/Quote]
哦,知道了,3Q
永远的明日 2009-05-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Vegertar 的回复:]
'ABC' ---->> "ABC"
[/Quote]
我是想问为什么'ABC'输出4276803,不是想输出ABC
  • 打赏
  • 举报
回复
这样输出:
cout << hex << 'ABC' << endl;


你就能大概揣摩一下你用的编译器是怎样处理'ABC'这种东西的。
BuleRiver 2009-05-02
  • 打赏
  • 举报
回复
把'ABC'改成"ABC"

65,211

社区成员

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

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