C limits.h Header
Implementation-defined limits for integer types
📏 What is limits.h?
The limits.h header defines constants that specify the ranges of integer types on your system, helping you write portable code that works across different platforms.
#include <limits.h>
#include <stdio.h>
int main() {
printf("Max int: %d\n", INT_MAX);
printf("Min int: %d\n", INT_MIN);
return 0;
}
Output:
Min int: -2147483648
Key limits.h Constants
Integer Limits
Maximum and minimum values for int types
INT_MAX, INT_MIN
LONG_MAX, LONG_MIN
Character Limits
Ranges for character data types
CHAR_MAX, CHAR_MIN
UCHAR_MAX
Short Limits
Boundaries for short integer types
SHRT_MAX, SHRT_MIN
USHRT_MAX
Bit Information
Number of bits in character types
CHAR_BIT
MB_LEN_MAX
🔹 Integer Type Limits
Understanding integer type limits through constants like INT_MAX, INT_MIN, and LONG_MAX enables overflow detection and range validation. The limits.h header defines these constants for all integer types on the current system. Query limits before operations to prevent overflow and implement appropriate safeguards. Use these values in overflow detection algorithms and input validation routines. Understanding integer limits is fundamental for writing robust, portable code that handles edge cases correctly.
#include <limits.h>
#include <stdio.h>
int main() {
printf("=== Integer Limits ===\n");
printf("INT_MAX: %d\n", INT_MAX);
printf("INT_MIN: %d\n", INT_MIN);
printf("\n=== Long Limits ===\n");
printf("LONG_MAX: %ld\n", LONG_MAX);
printf("LONG_MIN: %ld\n", LONG_MIN);
printf("\n=== Unsigned Limits ===\n");
printf("UINT_MAX: %u\n", UINT_MAX);
printf("ULONG_MAX: %lu\n", ULONG_MAX);
return 0;
}
Output:
INT_MAX: 2147483647
INT_MIN: -2147483648
=== Long Limits ===
LONG_MAX: 9223372036854775807
LONG_MIN: -9223372036854775808
=== Unsigned Limits ===
UINT_MAX: 4294967295
ULONG_MAX: 18446744073709551615
🔹 Character Type Limits
Exploring the ranges of character data types including signed and unsigned char reveals storage capabilities and potential representation issues. CHAR_MAX, CHAR_MIN, SCHAR_MAX, SCHAR_MIN, and UCHAR_MAX define character type boundaries. Understanding these limits prevents unintended sign extension or truncation. Character limits affect string processing algorithms and binary data handling. This knowledge enables you to make informed decisions about character type selection and implement appropriate validation logic.
#include <limits.h>
#include <stdio.h>
int main() {
printf("=== Character Limits ===\n");
printf("CHAR_BIT: %d bits\n", CHAR_BIT);
printf("CHAR_MAX: %d\n", CHAR_MAX);
printf("CHAR_MIN: %d\n", CHAR_MIN);
printf("\n=== Signed/Unsigned Char ===\n");
printf("SCHAR_MAX: %d\n", SCHAR_MAX);
printf("SCHAR_MIN: %d\n", SCHAR_MIN);
printf("UCHAR_MAX: %d\n", UCHAR_MAX);
return 0;
}
Output:
CHAR_BIT: 8 bits
CHAR_MAX: 127
CHAR_MIN: -128
=== Signed/Unsigned Char ===
SCHAR_MAX: 127
SCHAR_MIN: -128
UCHAR_MAX: 255
🔹 Overflow Detection
Using integer limits to detect potential overflow before it happens prevents silent errors and security vulnerabilities in numerical calculations. Before performing arithmetic, check whether values will exceed type limits: if (a > INT_MAX - b) before a + b. This defensive programming technique catches problems early. Overflow detection is critical for financial calculations, cryptography, and any application where numerical accuracy matters. Implementing systematic overflow checks demonstrates professional programming discipline.
#include <limits.h>
#include <stdio.h>
int safe_add(int a, int b) {
// Check for overflow before adding
if (a > 0 && b > INT_MAX - a) {
printf("Overflow would occur!\n");
return INT_MAX;
}
if (a < 0 && b < INT_MIN - a) {
printf("Underflow would occur!\n");
return INT_MIN;
}
return a + b;
}
int main() {
int result1 = safe_add(INT_MAX, 1);
int result2 = safe_add(100, 200);
printf("Safe result: %d\n", result2);
return 0;
}
Output:
Safe result: 300