C++ Reference ยท Types and Language Concepts
Fixed-width integer types (<cstdint>)
Reference for C++ fixed-width integer types in <cstdint>: int8_t to uint64_t, fast and least types, intptr_t, limits macros, literals and pitfalls.
The header <cstdint> defines integer types with a guaranteed size, so a std::int32_t is exactly 32 bits on every platform that provides it. They are the right tool for file formats, network protocols, hashes, hardware registers and anywhere else where the bit width is part of the specification, instead of guessing what int or long mean on this compiler.
The exact-width types are optional: a platform without an 8-bit byte could not provide std::int8_t. Every mainstream platform does. The header also defines "least" and "fast" families that are always available, plus the widest types and pointer-sized integers.
#include <cstdint> std::uint8_t flags = 0b1010'0001; std::int64_t big = 9'000'000'000; std::uint32_t hash = 2166136261u;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
std::intN_t std::uintN_t // N = 8, 16, 32, 64: exact width (optional) std::int_leastN_t std::uint_leastN_t // smallest type with at least N bits std::int_fastN_t std::uint_fastN_t // fastest type with at least N bits std::intmax_t std::uintmax_t // widest types std::intptr_t std::uintptr_t // can hold a pointer (optional)
| Family | Guarantee |
|---|---|
intN_t, uintN_t |
Exactly N bits, no padding, two's complement for the signed ones. Provided only if the platform has such a type. |
int_leastN_t |
The smallest signed type with at least N bits. Always provided for 8, 16, 32 and 64. |
int_fastN_t |
A type that is usually fastest for arithmetic with at least N bits. Its size can be larger than N (on x86-64 Linux int_fast16_t is 8 bytes). |
intmax_t, uintmax_t |
The widest integer types the implementation supports. |
intptr_t, uintptr_t |
An integer that can round-trip a void*. |
| Macros | INTN_MIN, INTN_MAX, UINTN_MAX and the equivalents for least/fast/max/ptr. INT64_C(x) and UINT64_C(x) make literals of the right type. |
Errors / undefined behavior: signed overflow of std::intN_t is still undefined, because they are aliases for ordinary types. Converting between pointers and integers other than through uintptr_t is implementation-defined.
Usage notes
- They are typedefs, not new types.
std::int32_tis usually justint,std::int64_tislongon Linux andlong longon Windows. Overloads on both may collide or differ by platform. std::int8_tandstd::uint8_tprint as characters throughstd::coutbecause they are usuallysigned charandunsigned char. Use+xorstatic_cast<int>(x)to print numbers.- Prefer
std::uint8_tfor byte values,std::bytefor raw memory (it has no arithmetic by accident). - Use
std::size_t/std::ptrdiff_tfor sizes and index differences, notstd::int32_t; 64-bit sizes do not fit. - Use
intwhen the width does not matter. Fixed-width types everywhere make arithmetic promote tointanyway, and add casts. - Endianness is not covered. A
std::uint32_thas the byte order of the machine; convert withstd::byteswap(C++23) or shifts when reading binary formats. - In C++ code use
<cstdint>(names instd::); the C header<stdint.h>also works and defines the names globally. Both exist since C++11. - Literal suffixes
u,l,ll, anduz(C++23, forstd::size_t) pick the literal type; see Literals.