Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Types and Literals

C fixed-width integers: int32_t, uint64_t and friends

Reference for the C fixed-width integer types from stdint.h: intN_t, uintN_t, least and fast variants, limit macros, literal macros and inttypes.h printing.

The types int, long and short have sizes that change from one platform to another. When a program must be sure of how many bits a value has, as in file formats, network protocols, hardware registers and hashes, it uses the fixed-width integer types from <stdint.h>: int8_t, int16_t, int32_t, int64_t and their unsigned partners uint8_t to uint64_t.

The exact-width types are optional in theory (a machine without an 8-bit byte cannot provide int8_t) but present on every platform you are likely to meet. They are typedefs for the built-in types, so int32_t is usually just another name for int.

C
#include <stdint.h>

uint8_t  flags = 0xA5;
int32_t  count = -2000000000;
uint64_t id = 1ULL << 40;

Try it

C Exact sizes and wraparound

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 scanf or fgets), it gets the next line from this box.

C Packing and unpacking bytes

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 scanf or fgets), it gets the next line from this box.

Syntax

C
#include <stdint.h>
Family Examples Meaning
Exact width int8_t, int16_t, int32_t, int64_t, uint8_t ... uint64_t Exactly N bits, two's complement for signed, no padding. Optional.
Least width int_least8_t ... uint_least64_t Smallest type with at least N bits. Required.
Fastest int_fast8_t ... uint_fast64_t Fastest type with at least N bits. Required.
Largest intmax_t, uintmax_t The widest integer types.
Pointer-sized intptr_t, uintptr_t See intptr_t.

Limit macros: INT8_MIN, INT8_MAX, UINT8_MAX, INT16_MIN, ... UINT64_MAX, SIZE_MAX, INTMAX_MAX. Literal macros: INT32_C(5), UINT64_C(1), INTMAX_C(...) give a constant of the right type. C23 adds width macros such as INT32_WIDTH and UINT64_WIDTH.

Printing: <inttypes.h> defines format macros: PRId32, PRIu64, PRIx8, and for scanf, SCNd32, SCNu64. They are string pieces glued between literals: "%" PRIu64 "\n".

Errors / undefined behavior: signed fixed-width arithmetic overflows like int (undefined). Arithmetic on int8_t and int16_t is promoted to int, so sums do not overflow until stored back.

Usage notes

  • Choose by meaning. Use fixed-width types for data layout and protocols; use int for loop counters and small values; use size_t for sizes and indices.
  • Do not assume a struct matches a file format just because its members are fixed-width: padding and byte order still apply. See Struct padding and Endianness.
  • uint8_t is usually unsigned char, so it is allowed to alias any object, but this is not guaranteed by the standard for the typedef alone.
  • int_fast types can be surprising, for example 64 bits on glibc x86-64 for int_fast16_t, which makes arrays of them larger than expected.
  • Match printf specifiers. %d with an int64_t is undefined behavior on platforms where int64_t is long. Use PRId64.
  • Mixed signedness follows the usual conversion rules; see unsigned types.
  • Added in C99 (<stdint.h>, <inttypes.h>). C++ spells them std::int32_t in <cstdint>.