加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sizeof(‘ab’)是否等于C中的sizeof(int)?

发布时间:2020-12-16 10:37:40 所属栏目:百科 来源:网络整理
导读:考虑到我有以下程序来确定多字节字符的大小. #includeiostreamint main(){ std::cout"size of multibyte characters : "sizeof('ab')std::endl;} 我的GCC编译器输出为4. 所以我有以下问题: 多字节字符文字的大小是多少? sizeof(‘ab’)是否等于sizeof(int)
考虑到我有以下程序来确定多字节字符的大小.

#include<iostream>

int main()
{
   std::cout<<"size of multibyte characters : "<<sizeof('ab')<<std::endl;
}

我的GCC编译器输出为4.

所以我有以下问题:

>多字节字符文字的大小是多少?
> sizeof(‘ab’)是否等于sizeof(int)?

解决方法

这是一个所谓的多字符文字,与单个字符对应物不同,它不是char类型,而是int类型(假设它是受支持的).按照 [lex.ccon]/2的规定,强调我的:

A character literal that does not begin with u8,u,U,or L is an
ordinary character literal. An ordinary character literal that
contains a single c-char representable in the execution character set
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
,or an ordinary character literal
containing a single c-char not representable in the execution
character set,is conditionally-supported,has type int,and has an
implementation-defined value.

所以你打印sizeof(int),就像你怀疑的那样.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读