一步一个脚印,走在自己前面!
想想自己5年后会是什么样子……
posts - 34, comments - 25, trackbacks - 0, articles - 6
C++博客
::
首页
::
新随笔
::
联系
::
聚合
::
管理
输出字符'A'/'a'的解析
Posted on 2007-01-15 10:59
仄洛
阅读(574)
评论(0)
编辑
收藏
引用
所属分类:
C++ Performance
输出字符'A'/'a'的代码段:
1
//
Test1: Directly insert 0x0A and 0x41 to ostream
2
cout
<<
0x0A
;
//
print type int 10
3
4
cout
<<
'
'
;
5
6
cout
<<
0x41
;
//
print type int 65, which is decimal number of 'A'.
7
cout
<<
'
'
;
8
9
//
Test2: Directly insert '\x41' to ostream
10
cout
<<
'
\x41
'
;
//
'\x41' is transformed char, which means type char 'A'.
11
//
So, it prints type char 'A' on screen.
12
cout
<<
'
'
;
13
14
//
Test3: First assign 0x41 to char variable, then output it.
15
char
char_out
=
0x41
;
//
char_out is equal 'A' which is type int 65.
16
cout
<<
char_out;
//
it prints type char 'A' on screen.
17
18
cout
<<
'
'
;
19
//
Test4: First cast 0x0A to char, then output it.
20
cout
<<
static_cast
<
char
>
(
0x41
); // it prints type char 'A' on screen.
21
22
cout
<<
'
'
;
23
//
Test5: Directly insert hexdecimal type number of 'A'
24
cout
<<
std::hex
/*
<< std::showbase
*/
<<
0x41
;
//
it prints string literal "41"
25
cout
<<
'
'
<<
std::hex
<<
0x0A
;
//
it prints type char 'a' on screen.
26
int
int_out
=
'
0x41
'
;
27
cout
<<
int_out;
//
it prints type int 8342**
28
29
cout
<<
endl;
0x41是字符'A'的16进制表示方式,它的10进制数是65,\x41是字符'A'的转义字符。而0x0A跟字符'A'没有丝毫关系,顶多就是0x0A借用了字符'A'而已。故如果想在屏幕上输出字符'A'可将其转义字符直接插入输出流中,或者将0x41转化为字符类型,然后插入,不得使用0x41直接插入,因为0x41本身是int类型的10进制数65。同时也不可以将'0x41'直接插入输出流,因为'0x41'是int类型的数。
可以尝试使用控制符std::hex来插入字符'a'到输出流。
标题
姓名
主页
验证码
*
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
.NET频道
博客园社区
闪存
相关文章:
编译期的依赖性系列
编译器何时为你产生默认构造函数
输出字符'A'/'a'的解析
头文件中的名称
The Return Value Optimization[2]
The Return Value Optimization[1]
Template and Inheritance
关于vector的使用
修炼成C++高手必看的C++书单
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理
Powered by:
C++博客
Copyright © 仄洛
日历
<
2009年7月
>
日
一
二
三
四
五
六
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(6)
给我留言
查看公开留言
查看私人留言
随笔分类
C++ Performance (9)
Computer Graphics and Its Algorithm (2)
Experience and Thought(10)
Game development using C++
Software development using C++(12)
随笔档案
2007年5月 (6)
2007年3月 (3)
2007年1月 (3)
2006年12月 (11)
2006年11月 (3)
2006年10月 (3)
2006年9月 (5)
文章档案
2006年10月 (2)
2006年9月 (4)
相册
Qt GUI programming basic
经典黑白图片
经典美女展
汽车模型展
收藏夹
C++ Core Language
C++ STL
CG Bolg
badkeeper's CG Blog
Computer Graphics Blog -- zerolee
搜索
最新评论
1. re: DLL编程专题
写的好。我转载了
--pathway
2. re: DLL编程专题
引用:"需要说明的是,隐式链接方式访问DLL时,在程序启动时也是通过LoadLibrary函数加载该进程需要的动态链接库的。"
请问有什么依据么?
--lollipop11
3. re: 从非MFC扩展DLL中导入一个自定义的类
有没有搞露掉一些步骤啊?不行啊!!
--Hoheart
4. re: 如何做一名好的开发人员
受教了,很有同感
--至尊拖鞋
5. re: 头文件中的名称
good work!
--morningstar
阅读排行榜
1. DLL编程专题(3905)
2. 关于vector的使用(2208)
3. 读写磁盘文件专题--采用C/C++/MFC/WIN32(API)方式(2006)
4. Qt GUI Programming Basic(1353)
5. 修炼成C++高手必看的C++书单(1007)
评论排行榜
1. DLL编程专题(6)
2. 求N个三角形的最大最小内切圆的方法(4)
3. 将电脑和银行结合在一起,你会得到什么?(3)
4. 建造原型的代价(2)
5. 组件间的物理关系(2)