Fixed-size integer types

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The C Standard Library

    /

  4. Fixed-size integer types

Integer types

At the beginning of this course, we saw the signed and unsigned integer primitives (char, short, int, long, unsigned, unsigned long...). The size (In bytes) or those variable types is not fixed and may vary depending on the compiler used. The header <stdint.h> of the C Standard Library defines integer types with fixed sizes and integer types that are of a minimum size.

Aliases

The types defined in the header <stdint.h> are not actually types, but rather typedefs of signed/unsigned integer primitives.

For example, int16_t is a typedef of an integer type that is guaranteed to weight 2 octets (16 bits). A compiler might define int_16t to be a typedef of the type short if the compiler defines the type short as a 16 bits integer. An other compiler with which the type int is coded using 16 bits might make int_16t a typedef of int.

Note that the typedefs of fixed-size integers defined in the header <stdint.h> are optional. If the machine the compiler compiles to does not natively support one of those integer sizes, the typedef of the integer of that size will not be defined (Thus we will not be able to use it). For example, a processor which byte size is 9 bits would not support integers made of 8 bits, because, remember, a byte is the smallest amount of memory a processor can natively work with. If the smallest unit of memory it can directly handle is 9 bits, it obviously can not directly handle 8 bits memory units.

Integer types with fixed-sizes

Here are the aliases of the fixed-size integers:

Number of bitsSignedUnsigned
8int8_tuint8_t
16int16_tuint16_t
32int32_tuint32_t
64int64_tuint64_t
Maximum natively supportedintmax_tuintmax_t

Smallest types with a minimum size

The header <stdint.h> also defines two other groups of integer aliases. Those groups do not define fixed-size integers, but define integers which are guaranteed to be of a minimum size. The first group defines integers that are at least of the specified size and if the machine does not support integers of that size, the typedef is linked to the integer type with the closest size that is at least of the size specified.

Here are the aliases of that group:

Minimum size (In bits)SignedUnsigned
8int_least8_tuint_least8_t
16int_least16_tuint_least16_t
32int_least32_tuint_least32_t
64int_least64_tuint_least64_t

Types optimized for performance

The next group defines aliases to integers that are at least of the specified size, but that are not necessarily the closest, but rather the fastest integer type that is at least of the specified size. Let us say we define a variable using the typedef of that group for an integer of at least 32 bits. If the machine supports 32 bits integers, but executes instructions faster when using 64 bits integers, the alias will point to a 64 bits integer.

Here are the aliases of that group:

Minimum size (In bits)SignedUnsigned
8int_fast8_tuint_fast8_t
16int_fast16_tuint_fast16_t
32int_fast32_tuint_fast32_t
64int_fast64_tuint_fast64_t