printf("%d",sizeof('a'));为什么结果是4?

小魔菇 2009-04-13 09:54:48
printf("%d",sizeof('a'));为什么结果是4?
char的长度是1吧
为什么输出的结果是4呢?
是不是因为%d的原因,被类型转换了?

在这里,sizeof('a')的值是1啊,就算被转换成int型 值也应该还是1才对
怎么会变成4?
还请高手指教一下。
...全文
1353 55 打赏 收藏 转发到动态 举报
写回复
用AI写文章
55 条回复
切换为时间正序
请发表友善的回复…
发表回复
kostion 2009-04-13
  • 打赏
  • 举报
回复
顶~
baihacker 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 49 楼 eclipse_2 的回复:]

哪儿有下的吗?
我还是个新手 对这些标准文档不是很了解
那位大哥有空的时候可以发份给我吗?
我的邮箱sunkang_2@yahoo.com.cn
[/Quote]
done
小魔菇 2009-04-13
  • 打赏
  • 举报
回复

哪儿有下的吗?
我还是个新手 对这些标准文档不是很了解
那位大哥有空的时候可以发份给我吗?
我的邮箱sunkang_2@yahoo.com.cn
flameearth 2009-04-13
  • 打赏
  • 举报
回复 1
字符'a' 这样 计算的是ASCII码 ASCII是个整数阿 所以计算就是按照int的大小 所以就是4了
ypb362148418 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mengde007 的回复:]
貌似Int型是4个字节吧
[/Quote]


你也知道强制转换,那就是4个
baihacker 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 45 楼 eclipse_2 的回复:]

WOBUGUAN 兄
你说的哪个什么ISO/IEC标准 从哪儿弄到的
可不可以给我一份啊?
[/Quote]
这些东西要常备...
C中对char literal的说明我忘了贴了,不过42楼说了
ISO/IEC 9899:1999 (E)

P61
10 An integer character constant has type int. The value of an integer character constant
containing a single character that maps to a single-byte execution character is the
numerical value of the representation of the mapped character interpreted as an integer.
The value of an integer character constant containing more than one character (e.g.,
'ab'), or containing a character or escape sequence that does not map to a single-byte
execution character, is implementation-defined. If an integer character constant contains
a single character or escape sequence, its value is the one that results when an object with
type char whose value is that of the single character or escape sequence is converted to
type int.
小魔菇 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 42 楼 WOBUGUAN 的回复:]
恩。是我弄错了。
C标准ISO/IEC 9899:TC3是这样说的:
An integer character constant has type int
所以GCC输出4是对的。

C++标准ISO/IEC 14882:2003(E)是这样说的:
An ordinary character literal that contains a single c-char has type
char
所以你的代码如果存作cpp就会输出1。

我也学到了不少东西呢。
[/Quote]
WOBUGUAN 兄
你说的哪个什么ISO/IEC标准 从哪儿弄到的
可不可以给我一份啊?
Paradin 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lingyin55 的回复:]
c语言中的字符常数是int型的,因此sizeof('a')实际上是sizeof(int),这是与c++不同的地方之一
[/Quote]
这个是对的。
liubuweiright 2009-04-13
  • 打赏
  • 举报
回复
飞雪~~~

无语.
WOBUGUAN 2009-04-13
  • 打赏
  • 举报
回复
恩。是我弄错了。
C标准ISO/IEC 9899:TC3是这样说的:
An integer character constant has type int
所以GCC输出4是对的。

C++标准ISO/IEC 14882:2003(E)是这样说的:
An ordinary character literal that contains a single c-char has type
char
所以你的代码如果存作cpp就会输出1。

我也学到了不少东西呢。
ouzhf 2009-04-13
  • 打赏
  • 举报
回复
up!
小魔菇 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 WOBUGUAN 的回复:]
白黑客热心,不过楼主怕是看不了那么多英文
[/Quote]
呵呵
也要硬着头皮看啊
小魔菇 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 CoolChina 的回复:]
你用的是VC吧,使用d%输出char类型的会被转换成int类型的吧
[/Quote]
不是哈 我的环境是
[^_^root@localhost /programme/apue]>gcc -v
.
.
.
gcc 版本 4.1.2 20080704 (Red Hat 4.1.2-44)
小魔菇 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 35 楼 WOBUGUAN 的回复:]
是吗?GCC4.1.2在RedHat 4.1.2下输出了4?
那你不要急着结这个帖子,让我也试一下。
[/Quote]

WOBUGUAN 2009-04-13
  • 打赏
  • 举报
回复
白黑客热心,不过楼主怕是看不了那么多英文
CoolChina 2009-04-13
  • 打赏
  • 举报
回复
你用的是VC吧,使用d%输出char类型的会被转换成int类型的吧
WOBUGUAN 2009-04-13
  • 打赏
  • 举报
回复
是吗?GCC4.1.2在RedHat 4.1.2下输出了4?
那你不要急着结这个帖子,让我也试一下。
baihacker 2009-04-13
  • 打赏
  • 举报
回复
ISO/IEC 14882:2003(E)

P17
1 A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by
the letter L, as in L’x’. A character literal that does not begin with L is an ordinary character literal, also
referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type
char, with value equal to the numerical value of the encoding of the c-char in the execution character set.
An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter
literal has type int and implementation-defined value.
baihacker 2009-04-13
  • 打赏
  • 举报
回复
ISO/IEC 14882:2003(E)

P80
5.3.3 Sizeof [expr.sizeof]
1 The sizeof operator yields the number of bytes in the object representation of its operand. The operand
is either an expression, which is not evaluated, or a parenthesized type-id. The sizeof operator shall not
be applied to an expression that has function or incomplete type, or to an enumeration type before all its
enumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designates
a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the
result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [Note: in particular,
sizeof(bool) and sizeof(wchar_t) are implementation-defined.69) ] [Note: See 1.7 for
the definition of byte and 3.9 for the definition of object representation. ]

ISO/IEC 9899:1999 (E)

P80
3 When applied to an operand that has type char, unsigned char, or signed char,
(or a qualified version thereof) the result is 1.
When applied to an operand that has array
type, the result is the total number of bytes in the array.84) When applied to an operand
that has structure or union type, the result is the total number of bytes in such an object,
including internal and trailing padding.
小魔菇 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 WOBUGUAN 的回复:]
读我25楼的就好了
读这些掌柜的越读越糊涂
[/Quote]
呵呵
在gcc下输出的结果确实是4 不是 1
我觉得27楼的掌柜的讲的挺有道理的
加载更多回复(35)

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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