<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
Include the header, pick the macros for your type, then use them in bounds checks and portable prints.
Add #include <limits.h> at the top of the file.
Choose macros for your type (INT_*, LONG_*, UCHAR_*, …).
Guard arithmetic so results stay within *_MIN … *_MAX.
Match printf formats: %d signed, %u unsigned.
Piece
Required?
Role
#include <limits.h>
Yes
Provides the limit macros
INT_MAX / INT_MIN
Often
Bounds for signed int
CHAR_BIT
No
Bits 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
Reference
Cheat Sheet
Typical 32-bit int / 64-bit long (LP64). Always trust the macros on your machine.
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.
Trace
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 ✓
Code
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.
Include limits.h. The compiler fills in platform-specific constants.
Print signed range.INT_MAX / INT_MIN bound every int value.
Print unsigned max. Use %u for UINT_MAX—not %d.
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;
}
Output
100 + 50 = 150
INT_MAX + 1: overflow avoided
How It Works
Positive b. If a > INT_MAX - b, the sum would wrap—return error instead.
Negative b. Symmetric check against INT_MIN prevents underflow.
Normal path.100 + 50 is safe → store 150.
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;
}
Output
INT_MAX with %d: 2147483647
UINT_MAX with %u: 4294967295
UCHAR_MAX: 255
SCHAR_MAX: 127
SCHAR_MIN: -128
How It Works
Signed macros.INT_MAX and SCHAR_* pair with %d.
Unsigned macros.UINT_MAX needs %u or it may look negative.
Byte limits.UCHAR_MAX is 255; SCHAR_MAX is 127 on two’s complement.
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.
Analysis
Performance Notes
Approach
Cost
Best for
limits.h macros
Compile-time (zero runtime)
Portable bounds and sizes
Overflow guards
One or two comparisons
Safe arithmetic at API boundaries
Hard-coded constants
Fragile across platforms
Avoid 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.
Remember
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.