C limits.h Header

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is limits.h?

<limits.h> is a C standard header that defines macros for integer type limits—the minimum and maximum values of char, short, int, long, and their unsigned forms, plus CHAR_BIT.

Remember
#include <limits.h>

INT_MAX / INT_MIN   → largest / smallest int
UINT_MAX            → largest unsigned int
CHAR_BIT            → bits in a byte (usually 8)
LONG_MAX / ULONG_MAX → long ranges

values are implementation-defined
use macros — never hard-code 2147483647
UsePrefer
int range checksINT_MAX / INT_MIN
Fixed bit widths (int32_t)stdint.h
Floating-point limitsfloat.h

How to Use limits.h in C

Include the header, pick the macros for your type, then use them in bounds checks and portable prints.

  1. Add #include <limits.h> at the top of the file.
  2. Choose macros for your type (INT_*, LONG_*, UCHAR_*, …).
  3. Guard arithmetic so results stay within *_MIN … *_MAX.
  4. Match printf formats: %d signed, %u unsigned.
PieceRequired?Role
#include <limits.h>YesProvides the limit macros
INT_MAX / INT_MINOftenBounds for signed int
CHAR_BITNoBits per byte; size in bits = sizeof(T) * CHAR_BIT

Pseudocode

Pseudocode
include limits.h

// safe add into int
if b > 0 and a > INT_MAX - b:
    overflow
else if b < 0 and a < INT_MIN - b:
    underflow
else:
    result = a + b

// print limits for your platform
print INT_MAX, INT_MIN, CHAR_BIT, UINT_MAX

Cheat Sheet

Typical 32-bit int / 64-bit long (LP64). Always trust the macros on your machine.

TopicDetail
Header#include <limits.h> (macros only)
CHAR_BITUsually 8
INT_MAXOften 2147483647 (32-bit int)
INT_MINOften -2147483648
UINT_MAXOften 4294967295 — print with %u
LONG_MAXPlatform-dependent (32- or 64-bit)
SCHAR_MAX127 (signed char always)
Fixed widthPrefer stdint.h (INT32_MAX)

Live Preview

Explore typical limits.h values and a safe-add overflow check (browser simulation of common 32-bit int).

Simulates typical desktop limits (same ideas as the C examples below).

Result
INT_MAX = 2147483647
INT_MIN = -2147483648

Typical 32-bit signed int range.
Use these macros — do not hard-code.

Worked Walkthrough — Safe Add

Before a + b into an int, check that the sum cannot exceed INT_MAX or go below INT_MIN.

Calculation
INT_MAX = 2147483647

Call 1: a = 100, b = 50
  b > 0 and a > INT_MAX - b?
  100 > 2147483647 - 50?  → false
  result = 150 ✓

Call 2: a = INT_MAX, b = 1
  b > 0 and a > INT_MAX - b?
  2147483647 > 2147483647 - 1?
  2147483647 > 2147483646  → true
  overflow — do not add ✓

C Examples

Compile with gcc file.c -std=c11 -o out. Sample output matches a typical 32-bit int desktop.

Example 1 — Print Key Limits

Discover INT_MAX, INT_MIN, CHAR_BIT, and UINT_MAX on your platform.

C
#include <limits.h>
#include <stdio.h>

int main(void) {
    printf("CHAR_BIT: %d\n", CHAR_BIT);
    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);

    printf("\nint bits: %zu\n", sizeof(int) * CHAR_BIT);

    return 0;
}

How It Works

  1. Include limits.h. The compiler fills in platform-specific constants.
  2. Print signed range. INT_MAX / INT_MIN bound every int value.
  3. Print unsigned max. Use %u for UINT_MAX—not %d.
  4. Compute bit width. sizeof(int) * CHAR_BIT is portable (here, 32).

Example 2 — Guard Against Overflow

Check before adding so a + b cannot exceed INT_MAX.

C
#include <limits.h>
#include <stdio.h>

int safe_add(int a, int b, int *out) {
    if (b > 0 && a > INT_MAX - b) {
        return -1;  /* would overflow */
    }
    if (b < 0 && a < INT_MIN - b) {
        return -1;  /* would underflow */
    }
    *out = a + b;
    return 0;
}

int main(void) {
    int result;

    if (safe_add(100, 50, &result) == 0) {
        printf("100 + 50 = %d\n", result);
    }

    if (safe_add(INT_MAX, 1, &result) != 0) {
        printf("INT_MAX + 1: overflow avoided\n");
    }

    return 0;
}

How It Works

  1. Positive b. If a > INT_MAX - b, the sum would wrap—return error instead.
  2. Negative b. Symmetric check against INT_MIN prevents underflow.
  3. Normal path. 100 + 50 is safe → store 150.
  4. Edge path. INT_MAX + 1 is rejected before undefined wraparound.

Example 3 — Signed vs Unsigned Print

Match the format specifier to the type so limit macros print correctly.

C
#include <limits.h>
#include <stdio.h>

int main(void) {
    printf("INT_MAX  with %%d: %d\n", INT_MAX);
    printf("UINT_MAX with %%u: %u\n", UINT_MAX);
    printf("UCHAR_MAX:        %u\n", (unsigned)UCHAR_MAX);
    printf("SCHAR_MAX:        %d\n", SCHAR_MAX);
    printf("SCHAR_MIN:        %d\n", SCHAR_MIN);

    return 0;
}

How It Works

  1. Signed macros. INT_MAX and SCHAR_* pair with %d.
  2. Unsigned macros. UINT_MAX needs %u or it may look negative.
  3. Byte limits. UCHAR_MAX is 255; SCHAR_MAX is 127 on two’s complement.
  4. Plain char. Prefer SCHAR_* / UCHAR_* when signedness of char matters.

Edge Cases & Pitfalls

Check these before relying on integer limit macros in real code.

platform

Do not hard-code 2147483647

Embedded and older systems may use 16-bit int. Always read INT_MAX from limits.h.

printf

Match format to signedness

UINT_MAX with %d is wrong. Use %u, %lu, or cast carefully.

char

Plain char signedness varies

CHAR_MIN may be 0 or negative. Prefer SCHAR_* / UCHAR_* when you care.

width

Need exact bits? Use stdint.h

int is not always 32 bits. For fixed widths, use int32_t and INT32_MAX.

Performance Notes

ApproachCostBest for
limits.h macrosCompile-time (zero runtime)Portable bounds and sizes
Overflow guardsOne or two comparisonsSafe arithmetic at API boundaries
Hard-coded constantsFragile across platformsAvoid in portable code

Macros are free. Overflow checks are cheap compared with I/O—put them where user input or untrusted sizes enter your logic.

Key Takeaways

  • Role: portable macros for integer min/max and CHAR_BIT.
  • Safety: compare against INT_MAX / INT_MIN before risky math.
  • Print: %d signed, %u unsigned.
  • Width: exact sizes belong in stdint.h, not assumptions about int.

One line: limits.h answers “how big can this integer type get here?”—so your code stays portable and safer.

Frequently Asked Questions

limits.h defines macros for integer type limits: minimum and maximum values for char, short, int, long, and their unsigned forms, plus CHAR_BIT (bits per byte). Use these macros instead of hard-coding numbers like 2147483647.
SCHAR_MIN and SCHAR_MAX always refer to signed char. CHAR_MIN and CHAR_MAX refer to plain char, which may be signed or unsigned depending on the compiler. Use SCHAR_* when you need signed char specifically.
No. An int might be 16 bits on embedded systems and 32 bits on desktops. LONG_MAX differs between platforms. Always use the macros from limits.h on your target.
limits.h names limits after fundamental types (INT_MAX, LONG_MAX). stdint.h provides fixed-width types (int32_t) and matching limits (INT32_MAX). Use limits.h for int/long; use stdint.h when you need exact bit widths.
CHAR_BIT is the number of bits in a byte (char). It is at least 8 and almost always exactly 8. Use CHAR_BIT with sizeof to compute bit sizes: sizeof(int) * CHAR_BIT.
UINT_MAX is an unsigned int. Printing it with %d can show a negative number. Match the printf format to the type: %d for signed, %u for unsigned, %ld/%lu for long.

Next: locale.h

Format numbers and text according to the user’s locale.

locale.h tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful