Fixed-width integer types are integer types from stdint.h whose size in bits is guaranteed, such as int32_t (exactly 32 bits, signed) and uint8_t (exactly 8 bits, unsigned). Plain int and long change size between platforms, so stdint.h types such as int32_t and uint8_t are the portable choice whenever a size matters: file formats, network protocols, hardware registers and binary data.
The header has been part of standard C since C99 and is available in C17 and C23. You include it with #include <stdint.h>, and you print the values with the macros from inttypes.h.
The C standard only promises minimum ranges: short and int hold at least 16 bits, long at least 32 bits and long long at least 64 bits. The real size depends on the compiler and the platform. A long is 8 bytes on 64-bit Linux and macOS but 4 bytes on 64-bit Windows.
If you write a struct to a file with int fields on one machine and read it on another, the layout can differ. That is the problem fixed-width types solve: the name tells you the size.
| Type | Meaning | Typical use |
|---|---|---|
int | At least 16 bits, usually 32 | Loop counters, general arithmetic |
long | At least 32 bits, 32 or 64 in practice | Avoid when size matters |
int32_t | Exactly 32 bits, signed | Binary formats, protocols |
uint8_t | Exactly 8 bits, unsigned | Raw bytes, pixel channels |
The exact-width types follow one naming pattern: an optional u for unsigned, int, the width in bits, then _t.
| Signed | Unsigned | Width |
|---|---|---|
int8_t | uint8_t | 8 bits |
int16_t | uint16_t | 16 bits |
int32_t | uint32_t | 32 bits |
int64_t | uint64_t | 64 bits |
The signed types use two's complement with no padding bits, so int8_t ranges from -128 to 127 and uint8_t from 0 to 255. These types are optional in the standard: if a platform has no integer type of exactly that width, the typedef does not exist. On every mainstream desktop, server and mobile platform they do exist, but a few unusual digital signal processors have no 8-bit type.
The program prints the same sizes on any platform that provides these types. The PRId32 and PRIu64 macros expand to the right printf length modifier for the platform, which is why they replace %d and %lu in portable code.
When you only need "at least this many bits", use the other two families. They are required by the standard for the widths 8, 16, 32 and 64.
int_least16_t and uint_least16_t are the smallest types with at least 16 bits. Use them when memory is tight.int_fast16_t and uint_fast16_t are the types the implementation considers fastest with at least 16 bits. Use them for hot loops where the exact size does not matter.On x86-64 Linux with glibc, int_fast16_t is 8 bytes because 64-bit operations are fast, while int_least16_t is 2 bytes. That difference is why you should not store fast types in files or structs shared between machines.
Two more families cover special sizes:
intptr_t and uintptr_t can hold a converted void * pointer. They are optional but nearly always present. Use uintptr_t for pointer arithmetic such as alignment checks.intmax_t and uintmax_t are the widest integer types the implementation has. They are required to exist.For sizes and array indexes, do not use these. Use size_t for object sizes and ptrdiff_t for pointer differences, because they are defined to match the address space.
stdint.h also defines the range of every type as macros: INT32_MIN, INT32_MAX, UINT8_MAX, INT64_MAX and so on. Use them instead of hard-coded numbers.
For integer literals of the right type, use the macros INT64_C(123) and UINT32_C(7). They add the correct suffix, such as L or UL, for the platform.
Fixed width does not remove the usual integer rules.
INT32_MAX + 1 on an int32_t is not guaranteed to wrap.uint8_t 255 plus 1 gives 0.int, such as uint8_t and int16_t, are promoted to int in expressions. In a << 24 with a uint8_t a, the result is computed as a signed int and can overflow into the sign bit. Cast to uint32_t first.Casting each byte to uint32_t before shifting avoids the promotion to signed int. When you read multi-byte values from a file or socket, also think about byte order: a fixed width tells you the size, not the endianness.
Use them for data that crosses a boundary: files, network packets, shared memory, hardware registers and APIs between languages. Use plain int for ordinary local counters and size_t for sizes. Do not turn every variable into int32_t; that adds noise and can be slower than the native word size on some targets.
No. On common platforms int is 32 bits, so int32_t is usually a typedef of int, but the standard does not require it. On some embedded targets int is 16 bits.
Not by guarantee. The exact-width types are optional, but the standard requires that if a type of that width exists with two's complement and no padding, the typedef must be provided. Use int_least32_t if you must support an unusual target.
Use the macro PRId64 from inttypes.h, written as "%" PRId64. A hard-coded %ld is wrong on Windows, where long is 32 bits.
On virtually all platforms yes, because unsigned char is the only 8-bit unsigned type. Be aware that the aliasing exception for character types is specified for char types, so treat uint8_t as a character type only when it is one.
Include stdint.h and use int32_t, uint8_t and the other exact-width types when the size of a value is part of the contract. Use the least and fast families when you need only a minimum width, uintptr_t for pointer-sized integers, and size_t for sizes. Print with the inttypes.h macros, build constants with INT64_C style macros, and remember that signed overflow and integer promotion still apply.