简体   繁体   中英

Is sizeof('ab') equal to sizeof(int) in C++?

Considering I have the following program that determines the size of multibyte characters.

#include<iostream>

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

My GCC compiler gives an output of 4.

So I have the following questions:

  • What is the size of multibyte characters literal?
  • Is sizeof('ab') equal to sizeof(int) ?

This is a so-called multicharacter literal, which unlike its single character counterpart, is not of type char , but of type int (assuming its supported). As specified in [lex.ccon]/2 , emphasis mine:

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.

So you print sizeof(int) , as you suspected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM