Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?
like image 839
msc Avatar asked Dec 11 '25 04:12

msc


1 Answers

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.

like image 192
StoryTeller - Unslander Monica Avatar answered Dec 13 '25 16:12

StoryTeller - Unslander Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!