<limits.h> provides platform-specific integer type limits: INT_MAX, INT_MIN, UINT_MAX, LONG_MAX, etc. Use these when writing portable code that works correctly regardless of whether integers are 32 or 64 bits.
Never hardcode values like 2147483647 directly; use the macros instead.
This example queries integer limits and demonstrates overflow checking.
// compile: gcc -o limitsexample limitsexample.c // run: ./limitsexample // description: query integer limits and check for overflow #include <limits.h> #include <stdio.h> int main() { printf("INT_MAX = %d\n", INT_MAX); printf("INT_MIN = %d\n", INT_MIN); printf("UINT_MAX = %u\n", UINT_MAX); printf("LONG_MAX = %ld\n", LONG_MAX); int a = INT_MAX, b = 1; if (a > INT_MAX - b) printf("INT_MAX + 1 would overflow\n"); unsigned int u = UINT_MAX; printf("UINT_MAX + 1 = %u (wraps to 0)\n", u + 1); return 0; }
Signed integer limits:
CHAR_MIN, CHAR_MAX: char (may be signed or unsigned depending on platform)SCHAR_MIN, SCHAR_MAX: signed charSHRT_MIN, SHRT_MAX: shortINT_MIN, INT_MAX: intLONG_MIN, LONG_MAX: longLLONG_MIN, LLONG_MAX: long longUnsigned integer limits:
UCHAR_MAX: unsigned charUSHRT_MAX: unsigned shortUINT_MAX: unsigned intULONG_MAX: unsigned longULLONG_MAX: unsigned long long (C99)Character and bit information:
CHAR_BIT: number of bits in char (typically 8)CHAR_MIN, CHAR_MAX: depends on signedness of char on platformMB_LEN_MAX: max bytes in multibyte characterPortable sizing:
<stdint.h> instead (e.g., INT32_MAX)<limits.h> defines platform-dependent limits