Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Types and Literals

Integer and floating-point limits in C

Reference for the range constants in limits.h, stdint.h and float.h: INT_MAX, UINT_MAX, SIZE_MAX, FLT_EPSILON, DBL_MAX and how to use them safely.

The size of int, long and double is not fixed by the C standard, only a minimum range is. Instead of hard-coding numbers like 2147483647, include <limits.h> for integer ranges and <float.h> for floating-point properties, and the compiler gives you the right values for the platform. Fixed-width types in <stdint.h> have their own limit macros.

Since C23, <limits.h> also defines BOOL_WIDTH, BITINT_MAXWIDTH and *_WIDTH macros that give the number of value bits of each integer type.

C
#include <limits.h>
#include <float.h>
int biggest = INT_MAX;
double tiny = DBL_EPSILON;

Try it

C 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 Floating-point limits and a safe overflow check

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.

Integer limit macros

Header Macros Meaning
<limits.h> CHAR_BIT Bits in a char (8 on every mainstream platform).
<limits.h> CHAR_MIN, CHAR_MAX Range of plain char (depends on its signedness).
<limits.h> SCHAR_MIN/MAX, UCHAR_MAX signed char and unsigned char.
<limits.h> SHRT_MIN/MAX, USHRT_MAX short.
<limits.h> INT_MIN/MAX, UINT_MAX int.
<limits.h> LONG_MIN/MAX, ULONG_MAX long.
<limits.h> LLONG_MIN/MAX, ULLONG_MAX long long.
<stdint.h> INT8_MAX ... INT64_MAX, UINT8_MAX ... UINT64_MAX Exact-width types (only when the type exists).
<stdint.h> INT_LEAST*_MAX, INT_FAST*_MAX Smallest and fastest types of at least N bits.
<stdint.h> INTPTR_MAX, INTMAX_MAX, SIZE_MAX, PTRDIFF_MAX Pointer-sized, widest and size types.

Floating-point macros

Each type has a family of macros with the prefix FLT_, DBL_ or LDBL_:

Suffix Meaning
MAX Largest finite value.
MIN Smallest positive normal value (not the most negative number).
TRUE_MIN Smallest positive subnormal value (C11).
EPSILON Difference between 1.0 and the next representable value.
DIG Decimal digits that survive a decimal-to-binary-to-decimal round trip.
MANT_DIG Binary digits in the significand.
MAX_EXP, MIN_EXP Largest and smallest exponent (base FLT_RADIX).
DECIMAL_DIG Digits needed to round-trip any value of the widest type.

Usage notes

  • FLT_MIN and DBL_MIN are not the most negative values. Use -DBL_MAX for that.
  • INT_MIN is one larger in magnitude than INT_MAX on two's complement systems, so -INT_MIN and abs(INT_MIN) overflow (undefined behavior).
  • Print with the matching specifier. %d for int, %ld for long, %zu for size_t, %e or %g for floats. Fixed-width types need either a cast or the PRId64 macros from <inttypes.h>.
  • Check before you overflow. Signed overflow is undefined; compare against the limits first, as add_overflows does above, or use C23 ckd_add.
  • Limits are compile-time constants, usable in static_assert and array sizes. CHAR_BIT * sizeof(int) is the bit count of int (although padding bits are possible in theory, INT_WIDTH in C23 is exact).
  • EPSILON is the spacing near 1.0 only. For larger numbers the gap grows proportionally; see comparing floating-point numbers.
  • The values differ by platform. long is 8 bytes on 64-bit Linux and 4 bytes on 64-bit Windows, so never assume a size without checking.