Skip to content
Tutorials Center
My learning

Topics

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.

C++
#include <cstdint>

std::uint8_t  flags = 0b1010'0001;
std::int64_t  big   = 9'000'000'000;
std::uint32_t hash  = 2166136261u;

Try it

C++ Exact widths, ranges and wrap-around

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.

C++ Least, fast, pointer-sized and widest types

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

C++
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_t is usually just int, std::int64_t is long on Linux and long long on Windows. Overloads on both may collide or differ by platform.
  • std::int8_t and std::uint8_t print as characters through std::cout because they are usually signed char and unsigned char. Use +x or static_cast<int>(x) to print numbers.
  • Prefer std::uint8_t for byte values, std::byte for raw memory (it has no arithmetic by accident).
  • Use std::size_t/std::ptrdiff_t for sizes and index differences, not std::int32_t; 64-bit sizes do not fit.
  • Use int when the width does not matter. Fixed-width types everywhere make arithmetic promote to int anyway, and add casts.
  • Endianness is not covered. A std::uint32_t has the byte order of the machine; convert with std::byteswap (C++23) or shifts when reading binary formats.
  • In C++ code use <cstdint> (names in std::); the C header <stdint.h> also works and defines the names globally. Both exist since C++11.
  • Literal suffixes u, l, ll, and uz (C++23, for std::size_t) pick the literal type; see Literals.