找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 267|回复: 2

Question8.9:I think something's wrong with my compiler。。。

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-1 13:55:46 | 显示全部楼层 |阅读模式

Q: I think something's wrong with my compiler: I just noticed that sizeof('a') is 2, not 1 (i.e. not sizeof(char)).
A: Perhaps surprisingly, character constants in C are of type int, so sizeof('a') is sizeof(int) (though this is another area where C++ differs). See also question 7.8.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-1 14:01:48 | 显示全部楼层
问题:我觉得我编译器出问题了:我注意到我的sizeof('a') 是2,而不是1(注意这里不是sizeof('a'))。
回答:你会惊奇的发现,字符常数在C中是整形变量,因此sizeof('a') 就为2了(这里作者是16位的编译器)(尽管这是跟C++中有区别的地方。在C++中字符常数还是被默认为char型的)。也可以参考问题7.8。

14

主题

65

回帖

0

积分

新手上路

积分
0
发表于 2013-7-2 00:15:57 | 显示全部楼层
呃,一般不会这么用,sizeof只作用在类型或者变量上好些。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 04:43 , Processed in 0.194126 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表