Is char guaranteed to be exactly 8-bit long?

Ori Popowski picture Ori Popowski · May 19, 2009 · Viewed 71.5k times · Source

That's all. Didn't find any similar topic so bear with me it there is.

Answer

Paul Dixon picture Paul Dixon · May 19, 2009

From a copy of the ANSI C specification, see Section 3.1.2.5 - Types:

An object declared as type char is large enough to store any member of the basic execution character set. If a member of the required source character set enumerated in $2.2.1 is stored in a char object, its value is guaranteed to be positive. If other quantities are stored in a char object, the behavior is implementation-defined: the values are treated as either signed or nonnegative integers.

The concept of "execution character set" is introduced in Section 2.2.1 - Character sets.

In other words, a char has to be at least big enough to contain an encoding of at least the 95 different characters which make up the basic execution character set.

Now add to that the section 2.2.4.2 - Numerical limits

A conforming implementation shall document all the limits specified in this section, which shall be specified in the headers <limits.h> and <float.h> .

Sizes of integral types

The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

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

  • minimum value for an object of type signed char
    SCHAR_MIN -127

  • maximum value for an object of type signed char
    SCHAR_MAX +127

  • maximum value for an object of type unsigned char
    UCHAR_MAX 255

....

So there you have it - the number of bits in a char must be at least 8.