C++ Standards : What is the size of a char in C++ ?

According to C++ 2011 standards ( ISO/IEC 14882 ) 1. The space of a char is specified in section 3.9.1 ( Fundamental Types ):

Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set

2. The basic character set is defined in section 2.3 ( Character Sets )

The basic source character set consists of 96 characters: the space character, the control characters representing horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters

3. However according to section 5.3.3 (Unary expressions/ sizeof), sizoef(char) should be 1 byte :

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1.

4. Based on this, if you check section 1.7 ( C++ Memory Model ) :

The fundamental storage unit in the C ++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set (2.3) and the eight-bit code units of the Unicode UTF-8  encoding form and is composed of a contiguous sequence of bits, the number of which is implementation  defined.

5.  C++ standards borrows from C standards and borrows CHAR_BIT macro which specifies number of bits in a byte in climits.h : Section C3 , Table 150 6. In C 99 standards ( ISO/IEC 9899:1999 ), minimum char bits is guaranteed to be 8 bits in section 5.2.4.2.1 ( Sizes of integer types) :

 number of bits for smallest object that is not a bit-field (byte) CHAR_BIT

Conclusion : In C++ standards, size of a char variable is guaranteed to be 1 byte. However 1 byte is not guranteed to be 8 bits, but to be at least 8 bits. Therefore sizeof a char is guaranteed to be at least 8 bits.

Advertisements

1 thought on “C++ Standards : What is the size of a char in C++ ?”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s