Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C stdint.h: fixed-width integer types

Reference for stdint.h: exact-width, least-width and fast integer types, intptr_t, intmax_t, the MIN/MAX limit macros, the INTn_C literal macros and C23 width macros.

The plain C types int, long and short have sizes that depend on the platform. <stdint.h> gives you integer types whose size you choose: int32_t is exactly 32 bits wherever it exists, uint8_t is exactly one 8-bit byte, and so on. It also defines the limits of all those types and macros that write constants of the right type.

Use these types for file formats, network protocols, hash functions, bit manipulation and anywhere a number of bits is part of the specification. Use plain int or size_t where the size does not matter.

C
#include <stdint.h>

uint8_t  byte  = 255;
int32_t  value = -40000;
uint64_t big   = UINT64_C(18446744073709551615);

Try it

C Exact sizes and limits

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 Least, fast, pointer-sized and maximum 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 scanf or fgets), it gets the next line from this box.

Syntax

C
#include <stdint.h>

intN_t   uintN_t           /* exactly N bits: N = 8, 16, 32, 64 (optional) */
int_leastN_t  uint_leastN_t   /* smallest type with at least N bits (required) */
int_fastN_t   uint_fastN_t    /* fastest type with at least N bits (required) */
intmax_t  uintmax_t        /* the widest integer types */
intptr_t  uintptr_t        /* can hold a void * round trip (optional) */
Macro Meaning
INTN_MIN, INTN_MAX, UINTN_MAX Limits of the exact-width types (INT32_MAX, UINT8_MAX, ...).
INT_LEASTN_MAX, INT_FASTN_MAX Limits of the least and fast types.
INTPTR_MAX, INTMAX_MAX, UINTMAX_MAX Limits of the pointer-sized and widest types.
SIZE_MAX, PTRDIFF_MAX, WCHAR_MAX, SIG_ATOMIC_MAX Limits of other library types.
INTN_C(c), UINTN_C(c), INTMAX_C(c) Expand to a constant of the corresponding type (at least int rank).
INTN_WIDTH, UINTN_WIDTH, INT_FASTN_WIDTH, ... C23: the width in bits.

Returns: these are type and macro definitions only.

Errors / undefined behavior: signed overflow is still undefined behavior, even for int32_t. Converting an out-of-range value to a signed type gives an implementation-defined result (GCC wraps modulo 2^N). Converting a pointer to intptr_t and back is guaranteed only for void *.

Usage notes

  • Exact-width types are optional. int8_t and uint8_t exist only if the machine has an 8-bit type with no padding. On any mainstream hardware they do, but on some DSPs with 16-bit bytes they do not. If you are writing portable code for those systems, use the least types.
  • Exact-width signed types are two's complement. The standard requires it for intN_t, and C23 requires it for every signed integer type.
  • Which to choose? int_leastN_t when memory matters (the smallest type), int_fastN_t when speed matters (a type the CPU handles efficiently; on 64-bit glibc, int_fast16_t and int_fast32_t are 8 bytes), and intN_t when the exact layout matters. For counts and sizes use size_t.
  • Do not print with %d. int64_t is long on Linux and long long on Windows. The portable way is the macros from inttypes.h (PRId64), or cast to long long and use %lld.
  • Small types promote. uint8_t + uint8_t is computed as int. Assign back to uint8_t to wrap, and watch out for sign extension when mixing signed and unsigned values.
  • Pointer round trip. uintptr_t is the right type for aligning addresses and storing tags in pointers, for example to check alignment with address % 16 == 0. Do not rely on its numerical value for anything else.
  • Constants. INT64_C(5) expands to 5L or 5LL as needed. A plain 1 << 40 is an int shift and overflows; UINT64_C(1) << 40 is correct.
  • Cost. All are plain integers; there is no overhead, only the effect of the width on registers and cache lines.
  • Standard version: C99. C23 adds the _WIDTH macros.