Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Integer limits and widths in <stdint.h>

Macros from <stdint.h> describe integer ranges and bit widths. Use them to check portability and reject oversized calculations before evaluating them. C23 adds the macros ending in _WIDTH.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdint.h>

/* Macro name patterns (substitute 8, 16, 32, or 64 for N):
   INTN_MIN, INTN_MAX, INTN_WIDTH
   UINTN_MAX, UINTN_WIDTH
   INT_LEASTN_MIN, INT_LEASTN_MAX, INT_LEASTN_WIDTH
   UINT_LEASTN_MAX, UINT_LEASTN_WIDTH
   INT_FASTN_MIN, INT_FASTN_MAX, INT_FASTN_WIDTH
   UINT_FASTN_MAX, UINT_FASTN_WIDTH

   Other macro names:
   INTPTR_MIN, INTPTR_MAX, INTPTR_WIDTH
   UINTPTR_MAX, UINTPTR_WIDTH
   INTMAX_MIN, INTMAX_MAX, INTMAX_WIDTH
   UINTMAX_MAX, UINTMAX_WIDTH
   PTRDIFF_MIN, PTRDIFF_MAX, PTRDIFF_WIDTH
   SIZE_MAX, SIZE_WIDTH
   SIG_ATOMIC_MIN, SIG_ATOMIC_MAX, SIG_ATOMIC_WIDTH
   WCHAR_MIN, WCHAR_MAX, WCHAR_WIDTH
   WINT_MIN, WINT_MAX, WINT_WIDTH
*/

<stdint.h> supplies constants usable in #if: _MIN and _MAX give inclusive bounds; C23 adds _WIDTH for bit width, including the sign but excluding padding bits. For width W, the signed range is −2^(W−1)…2^(W−1)−1 and the unsigned range is 0…2^W−1. The UINT families have no minimum macros. However, if SIG_ATOMIC_MIN, WCHAR_MIN, or WINT_MIN is provided for an unsigned type, its value is 0.

For each N = 8, 16, 32, 64, the syntax patterns cover every listed symbol. INTN_MIN, INTN_MAX, INTN_WIDTH, UINTN_MAX, UINTN_WIDTH describe exactly N bits without padding; these types and macros are optional. The INT_LEASTN_*/UINT_LEASTN_* families describe the smallest types with at least N bits; INT_FASTN_*/UINT_FASTN_* describe usually fastest types with at least N bits, not necessarily fastest for every task. Both families are required for these N and may be wider.

INTPTR_MIN, INTPTR_MAX, INTPTR_WIDTH and UINTPTR_MAX, UINTPTR_WIDTH describe optional intptr_t/uintptr_t, supporting a void * to integer and back round trip that preserves equality. INTMAX_MIN, INTMAX_MAX, INTMAX_WIDTH, UINTMAX_MAX, UINTMAX_WIDTH describe required intmax_t/uintmax_t; their greatest-range guarantee has exceptions for bit-precise types and certain wider extended types.

PTRDIFF_MIN, PTRDIFF_MAX, PTRDIFF_WIDTH describe ptrdiff_t (pointer differences); SIZE_MAX, SIZE_WIDTH describe size_t (sizes). The triples SIG_ATOMIC_MIN/SIG_ATOMIC_MAX/SIG_ATOMIC_WIDTH, WCHAR_MIN/WCHAR_MAX/WCHAR_WIDTH, WINT_MIN/WINT_MAX/WINT_WIDTH describe sig_atomic_t, wchar_t, wint_t, respectively. Do not assume their signedness. <stdint.h> need not declare these types from other headers; a freestanding environment may omit some.

Check optional macros with #ifdef. The example tests count <= SIZE_MAX / size before multiplication, also guarding against division by zero. Success means only that the product is representable, not that memory allocation will succeed.

Reference identifiers covered by this entry: INT8_MIN, INT8_MAX, INT8_WIDTH, UINT8_MAX, UINT8_WIDTH, INT_LEAST8_MIN, INT_LEAST8_MAX, INT_LEAST8_WIDTH, UINT_LEAST8_MAX, UINT_LEAST8_WIDTH, INT_FAST8_MIN, INT_FAST8_MAX, INT_FAST8_WIDTH, UINT_FAST8_MAX, UINT_FAST8_WIDTH, INT16_MIN, INT16_MAX, INT16_WIDTH, UINT16_MAX, UINT16_WIDTH, INT_LEAST16_MIN, INT_LEAST16_MAX, INT_LEAST16_WIDTH, UINT_LEAST16_MAX, UINT_LEAST16_WIDTH, INT_FAST16_MIN, INT_FAST16_MAX, INT_FAST16_WIDTH, UINT_FAST16_MAX, UINT_FAST16_WIDTH, INT32_MIN, INT32_MAX, INT32_WIDTH, UINT32_MAX, UINT32_WIDTH, INT_LEAST32_MIN, INT_LEAST32_MAX, INT_LEAST32_WIDTH, UINT_LEAST32_MAX, UINT_LEAST32_WIDTH, INT_FAST32_MIN, INT_FAST32_MAX, INT_FAST32_WIDTH, UINT_FAST32_MAX, UINT_FAST32_WIDTH, INT64_MIN, INT64_MAX, INT64_WIDTH, UINT64_MAX, UINT64_WIDTH, INT_LEAST64_MIN, INT_LEAST64_MAX, INT_LEAST64_WIDTH, UINT_LEAST64_MAX, UINT_LEAST64_WIDTH, INT_FAST64_MIN, INT_FAST64_MAX, INT_FAST64_WIDTH, UINT_FAST64_MAX, UINT_FAST64_WIDTH.

Examples

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

static int product_fits(size_t count, size_t size)
{
    return size == 0 || count <= SIZE_MAX / size;
}

int main(void)
{
#ifdef UINT_LEAST8_WIDTH
    _Static_assert(UINT_LEAST8_WIDTH >= 8, "width too small");
#else
    _Static_assert(UINT_LEAST8_MAX >= 255, "range too small");
#endif
    printf("small: %d\n", product_fits(3, 4));
    printf("overflow: %d\n", product_fits(SIZE_MAX, 2));
    return 0;
}

Result

small: 1
overflow: 0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources