C语言字符常量视为int类型,C语言笔记之一:字符常量integer character constant是int型...

C99中的描述:

"An integer character constant is a sequence of one or more multibyte characters enclosed

in single-quotes, as in 'x'. A wide character constant is the same, except prefixed by the

letter L. With a few exceptions detailed later, the elements of the sequence are any

members of the source character set; they are mapped in an implementation-defined

manner to members of the execution character set."

"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."

标准中明确规定了字符常量的类型为int,所以sizeof('a')的值为4,sizeof('abcde')的值同样为4。

"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."多字符的字符常量的值是由编译器来决定的:

int a = ‘a’;//a = 0x61

int a = ‘aaa’;//a的值取决于编译器的实现,一般是0x616161,gcc报警warning: multi-character character constant

int a = ‘baaaa’///a的值取决于编译器的实现,一般是0x61616161,gcc报警warning: character constant too long for its type

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值