Fixed-width integer types such as std::int32_t and std::uint64_t are integer types whose size in bits is exactly what the name says, on every platform that provides them. They are declared in the <cstdint> header and solve the problem that int and long change size between systems. Use them when a value must be exactly 8, 16, 32 or 64 bits wide, for example in a file format, a network packet or hashing code.
The "int32_t c++" family has been in the standard since C++11 and is part of the language you use every day in modern code.
The built-in types only promise minimum sizes. long is 64 bits on 64-bit Linux and 32 bits on 64-bit Windows, so code that stores a long in a binary file may be unreadable on another system. Fixed-width types remove the guesswork:
Each size is the same everywhere the types exist.
| Type | Meaning | Required to exist |
|---|---|---|
std::int8_t, std::uint8_t | Exactly 8 bits | Only if the platform has them |
std::int16_t, std::uint16_t | Exactly 16 bits | Only if the platform has them |
std::int32_t, std::uint32_t | Exactly 32 bits | Only if the platform has them |
std::int64_t, std::uint64_t | Exactly 64 bits | Only if the platform has them |
std::int_least32_t | Smallest type with at least 32 bits | Always |
std::int_fast32_t | Fastest type with at least 32 bits | Always |
std::intmax_t, std::uintmax_t | Largest integer types | Always |
std::intptr_t, std::uintptr_t | Integers that can hold a pointer | Optional |
The exact-width types are optional in the standard, because some unusual processors have no 8-bit or 32-bit integer. On every mainstream desktop, server and mobile target they exist, and a program that needs one will fail to compile on a platform that lacks it, which is the behaviour you want.
The names are in std, and the headers also usually provide the global names such as int32_t. Prefer the std:: form in C++ code.
When you do not need an exact size, the other two groups express intent better:
std::int_least16_t is the smallest type that has at least 16 bits. Use it when memory matters, such as large arrays.std::int_fast16_t is the type the implementation considers fastest with at least 16 bits. On some platforms it is wider than 16 bits.In practice, most code uses either the plain int for general arithmetic or the exact-width types for data layout. The least and fast variants are rare outside embedded and portable library code.
Each type has constants in <cstdint> such as INT32_MAX, and std::numeric_limits works too:
std::int8_t and std::uint8_t are normally aliases for signed char and unsigned char, so std::cout prints them as characters:
Cast to int (or use unary +) when you want the number.
A typical use is a struct that mirrors a file header:
Three details matter here:
static_assert checks the layout, because the compiler may add padding between members.std::memcpy is the safe way to copy bytes into an object. Casting a char buffer to a Header pointer breaks aliasing rules.std::endian in <bit> to check the platform order, and C++23 adds std::byteswap.std::int64_t is the same type as long long. On Linux it is usually long, on Windows long long. Overloads on long and long long can therefore behave differently across platforms.std::uint8_t a = 200, b = 100; auto c = a + b; gives 300, because both operands are promoted to int before adding. Storing the result back into a uint8_t wraps to 44.std::uint32_t for loop counters and sizes that interact with std::size_t, which causes sign and width warnings. Use std::size_t for container indexes.<cstdint>. It declares the names in std and normally also in the global namespace. The C-style header <stdint.h> also works.
Not guaranteed. On common platforms it is, but another platform could use a different type. The fixed-width name guarantees the size, not the underlying type.
C++17 added std::byte in <cstddef> for raw memory. It cannot be used in arithmetic by accident. Use std::uint8_t when the value is a small number.
They were added in C++11 through <cstdint>, based on C99, and many compilers supported them earlier.
Use std::int32_t, std::uint64_t and the other types from <cstdint> when a size must be exact, such as in file formats, protocols and hashing. Use int for general counts, std::size_t for indexes, and cast 8-bit values before printing. Remember that exact width does not fix byte order or padding, so verify layouts with static_assert.