Fixed-size integer types
Location
-
Courses
/
-
Complete C++ Course
/
-
The C Standard Library
/
Fixed-size integer types
Summary
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 bits | Signed | Unsigned |
---|---|---|
8 | int8_t | uint8_t |
16 | int16_t | uint16_t |
32 | int32_t | uint32_t |
64 | int64_t | uint64_t |
Maximum natively supported | intmax_t | uintmax_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) | Signed | Unsigned |
---|---|---|
8 | int_least8_t | uint_least8_t |
16 | int_least16_t | uint_least16_t |
32 | int_least32_t | uint_least32_t |
64 | int_least64_t | uint_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) | Signed | Unsigned |
---|---|---|
8 | int_fast8_t | uint_fast8_t |
16 | int_fast16_t | uint_fast16_t |
32 | int_fast32_t | uint_fast32_t |
64 | int_fast64_t | uint_fast64_t |