Skip to content
Tutorials Center
My learning

Topics

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

C limits.h: CHAR_BIT, INT_MAX and more

Reference for limits.h: the minimum and maximum values of char, short, int, long and long long, CHAR_BIT, MB_LEN_MAX, the C23 _WIDTH macros and BITINT_MAXWIDTH.

<limits.h> describes the ranges of the standard integer types on the platform your program is compiled for. Each type gets a MIN and a MAX macro (unsigned types only a MAX, because their minimum is 0), plus CHAR_BIT, the number of bits in a byte, and MB_LEN_MAX, the longest multibyte character.

The values are constant expressions, so you can use them in static_assert, array sizes and #if conditions. Use them instead of hard-coded numbers: INT_MAX is clearer than 2147483647 and stays correct if int has another size.

C
#include <limits.h>

int biggest = INT_MAX;
unsigned long all_ones = ULONG_MAX;

Try it

C The standard integer 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 C23 width macros and safe addition

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 <limits.h>

CHAR_BIT   MB_LEN_MAX
SCHAR_MIN  SCHAR_MAX  UCHAR_MAX  CHAR_MIN  CHAR_MAX
SHRT_MIN   SHRT_MAX   USHRT_MAX
INT_MIN    INT_MAX    UINT_MAX
LONG_MIN   LONG_MAX   ULONG_MAX
LLONG_MIN  LLONG_MAX  ULLONG_MAX
BOOL_WIDTH  CHAR_WIDTH  SCHAR_WIDTH  UCHAR_WIDTH  SHRT_WIDTH  USHRT_WIDTH
INT_WIDTH   UINT_WIDTH  LONG_WIDTH   ULONG_WIDTH  LLONG_WIDTH  ULLONG_WIDTH   /* C23 */
BITINT_MAXWIDTH                                                                 /* C23 */
Name Description
CHAR_BIT Number of bits in a char (at least 8; exactly 8 on virtually all current platforms).
CHAR_MIN, CHAR_MAX Range of plain char: SCHAR_MIN/SCHAR_MAX if char is signed, 0/UCHAR_MAX if unsigned.
INT_MIN and friends Range of each type. The standard guarantees at least: int 16 bits, long 32 bits, long long 64 bits.
*_WIDTH C23: bits in the type, counting the sign bit for signed types.
BITINT_MAXWIDTH C23: the largest N for which _BitInt(N) is supported.
MB_LEN_MAX Maximum bytes in a multibyte character of any supported locale.

Returns: all macros are constant expressions you can use in #if and static_assert.

Errors / undefined behavior: none for the macros. Computing INT_MAX + 1 or -INT_MIN is signed overflow, which is undefined behavior; check before you calculate.

Usage notes

  • Unsigned types wrap, signed types do not. UINT_MAX + 1u is 0, a defined result. INT_MAX + 1 is undefined; the compiler may assume it never happens.
  • INT_MIN has no positive counterpart on two's complement systems: -INT_MIN, abs(INT_MIN) and INT_MIN / -1 are all undefined. See abs, labs and div.
  • Prefer CHAR_BIT to the literal 8 when you compute bit counts (sizeof x * CHAR_BIT). It documents intent and survives unusual platforms.
  • Plain char is unsigned on some platforms (ARM Linux, for example) and signed on others (x86). Test with CHAR_MIN < 0, and use unsigned char for raw bytes.
  • long varies: 64 bits on 64-bit Linux and macOS, 32 bits on 64-bit Windows. Code that needs exactly 64 bits should use int64_t from stdint.h or long long.
  • Float limits are elsewhere. DBL_MAX, FLT_EPSILON and friends are in float.h; SIZE_MAX, INT32_MAX and the like are in <stdint.h>.
  • Preprocessor use. The MIN/MAX macros work inside #if, because they are replaced by plain integer constants, for example #if INT_MAX < 100000.
  • C23 _WIDTH macros are the only portable way to count the bits of int without assuming sizeof(int) * CHAR_BIT has no padding bits. GCC 15 provides them in C23 mode.
  • Standard version: C89; LLONG_* in C99; *_WIDTH and BITINT_MAXWIDTH in C23.