C++ Size of integral types


Example

The following types are defined as integral types:

  • char
  • Signed integer types
  • Unsigned integer types
  • char16_t and char32_t
  • bool
  • wchar_t

With the exception of sizeof(char) / sizeof(signed char) / sizeof(unsigned char), which is split between § 3.9.1.1 [basic.fundamental/1] and § 5.3.3.1 [expr.sizeof], and sizeof(bool), which is entirely implementation-defined and has no minimum size, the minimum size requirements of these types are given in section § 3.9.1 [basic.fundamental] of the standard, and shall be detailed below.

Size of char

All versions of the C++ standard specify, in § 5.3.3.1, that sizeof yields 1 for unsigned char, signed char, and char (it is implementation defined whether the char type is signed or unsigned).

C++14

char is large enough to represent 256 different values, to be suitable for storing UTF-8 code units.

Size of signed and unsigned integer types

The standard specifies, in § 3.9.1.2, that in the list of standard signed integer types, consisting of signed char, short int, int, long int, and long long int, each type will provide at least as much storage as those preceding it in the list. Furthermore, as specified in § 3.9.1.3, each of these types has a corresponding standard unsigned integer type, unsigned char, unsigned short int, unsigned int, unsigned long int, and unsigned long long int, which has the same size and alignment as its corresponding signed type. Additionally, as specified in § 3.9.1.1, char has the same size and alignment requirements as both signed char and unsigned char.

C++11

Prior to C++11, long long and unsigned long long were not officially part of the C++ standard. However, after their introduction to C, in C99, many compilers supported long long as an extended signed integer type, and unsigned long long as an extended unsigned integer type, with the same rules as the C types.

The standard thus guarantees that:

1 == sizeof(char)  == sizeof(signed char) == sizeof(unsigned char)
  <= sizeof(short) == sizeof(unsigned short)
  <= sizeof(int)   == sizeof(unsigned int)
  <= sizeof(long)  == sizeof(unsigned long)
C++11
 <= sizeof(long long) == sizeof(unsigned long long)

Specific minimum sizes for each type are not given by the standard. Instead, each type has a minimum range of values it can support, which is, as specified in § 3.9.1.3, inherited from the C standard, in §5.2.4.2.1. The minimum size of each type can be roughly inferred from this range, by determining the minimum number of bits required; note that for any given platform, any type's actual supported range may be larger than the minimum. Note that for signed types, ranges correspond to one's complement, not the more commonly used two's complement; this is to allow a wider range of platforms to comply with the standard.

TypeMinimum rangeMinimum bits required
signed char-127 to 127 (-(27 - 1) to (27 - 1))8
unsigned char0 to 255 (0 to 28 - 1)8
signed short-32,767 to 32,767 (-(215 - 1) to (215 - 1))16
unsigned short0 to 65,535 (0 to 216 - 1)16
signed int-32,767 to 32,767 (-(215 - 1) to (215 - 1))16
unsigned int0 to 65,535 (0 to 216 - 1)16
signed long-2,147,483,647 to 2,147,483,647 (-(231 - 1) to (231 - 1))32
unsigned long0 to 4,294,967,295 (0 to 232 - 1)32
C++11
TypeMinimum rangeMinimum bits required
signed long long-9,223,372,036,854,775,807 to 9,223,372,036,854,775,807 (-(263 - 1) to (263 - 1))64
unsigned long long0 to 18,446,744,073,709,551,615 (0 to 264 - 1)64

As each type is allowed to be greater than its minimum size requirement, types may differ in size between implementations. The most notable example of this is with the 64-bit data models LP64 and LLP64, where LLP64 systems (such as 64-bit Windows) have 32-bit ints and longs, and LP64 systems (such as 64-bit Linux) have 32-bit ints and 64-bit longs. Due to this, integer types cannot be assumed to have a fixed width across all platforms.

C++11

If integer types with fixed width are required, use types from the <cstdint> header, but note that the standard makes it optional for implementations to support the exact-width types int8_t, int16_t, int32_t, int64_t, intptr_t, uint8_t, uint16_t, uint32_t, uint64_t and uintptr_t.

C++11

Size of char16_t and char32_t

The sizes of char16_t and char32_t are implementation-defined, as specified in § 5.3.3.1, with the stipulations given in § 3.9.1.5:

  • char16_t is large enough to represent any UTF-16 code unit, and has the same size, signedness, and alignment as uint_least16_t; it is thus required to be at least 16 bits in size.

  • char32_t is large enough to represent any UTF-32 code unit, and has the same size, signedness, and alignment as uint_least32_t; it is thus required to be at least 32 bits in size.

Size of bool

The size of bool is implementation defined, and may or may not be 1.

Size of wchar_t

wchar_t, as specified in § 3.9.1.5, is a distinct type, whose range of values can represent every distinct code unit of the largest extended character set among the supported locales. It has the same size, signedness, and alignment as one of the other integral types, which is known as its underlying type. This type's size is implementation-defined, as specified in § 5.3.3.1, and may be, for example, at least 8, 16, or 32 bits; if a system supports Unicode, for example, wchar_t is required to be at least 32 bits (an exception to this rule is Windows, where wchar_t is 16 bits for compatibility purposes). It is inherited from the C90 standard, ISO 9899:1990 § 4.1.5, with only minor rewording.

Depending on the implementation, the size of wchar_t is often, but not always, 8, 16, or 32 bits. The most common examples of these are:

  • In Unix and Unix-like systems, wchar_t is 32-bit, and is usually used for UTF-32.
  • In Windows, wchar_t is 16-bit, and is used for UTF-16.
  • On a system which only has 8-bit support, wchar_t is 8 bit.
C++11

If Unicode support is desired, it is recommended to use char for UTF-8, char16_t for UTF-16, or char32_t for UTF-32, instead of using wchar_t.


Data Models

As mentioned above, the widths of integer types can differ between platforms. The most common models are as follows, with sizes specified in bits:

Modelintlongpointer
LP32 (2/4/4)163232
ILP32 (4/4/4)323232
LLP64 (4/4/8)323264
LP64 (4/8/8)326464

Out of these models:

  • 16-bit Windows used LP32.
  • 32-bit *nix systems (Unix, Linux, Mac OSX, and other Unix-like OSes) and Windows use ILP32.
  • 64-bit Windows uses LLP64.
  • 64-bit *nix systems use LP64.

Note, however, that these models aren't specifically mentioned in the standard itself.