HOWTO · C++
INT_MAX and INT_MIN Macro Expressions in C++
Learn how to read the implementation's int limits with INT_MIN and INT_MAX in C++, use them in a full-range random distribution, and reason safely about integer bounds.
On this page
INT_MIN and INT_MAX are the smallest and largest values representable by the implementation’s int type. Include <climits> to use these macros in C++; their values are implementation-dependent, so inspect them instead of assuming a particular platform’s numbers.
What INT_MIN and INT_MAX mean in C++
The two macros describe the range of int: INT_MIN is its lower bound and INT_MAX its upper bound. The range is set by the C++ implementation and is not a promise that every machine uses the same numeric values. These macros apply to int; they do not describe long, short, or unsigned integer types. The <climits> header also provides related macros such as LONG_MIN, LONG_MAX, and UINT_MAX when those types are relevant.
The macros let code refer to the actual limits selected by the implementation. This is more portable than embedding a familiar decimal value in a comparison or range definition. They report bounds; they do not make arithmetic safe by themselves.
Read integer limits with <climits>
Include <climits> for the integer limit macros. This complete example prints the two int bounds, with no assumed decimal values:
Run this example:
#include <climits>
#include <iostream>
int main() {
std::cout << "INT_MIN = " << INT_MIN << '\n';
std::cout << "INT_MAX = " << INT_MAX << '\n';
}
INT_MIN = -2147483648
INT_MAX = 2147483647
These are the values observed in the tested environment; your implementation may print different limits. If you need limits for another integer type, use its corresponding macro from <climits> rather than reusing the int bounds.
Generate a random int across its full range
std::uniform_int_distribution<int> accepts inclusive lower and upper bounds. Passing INT_MIN and INT_MAX therefore asks for values across the full range of int. A random engine supplies the sequence; the distribution maps engine results into the requested range.
Run this example:
#include <climits>
#include <iostream>
#include <random>
int main() {
std::mt19937 engine(42);
std::uniform_int_distribution<int> distribution(INT_MIN, INT_MAX);
for (int count = 0; count < 5; ++count) {
std::cout << distribution(engine) << '\n';
}
}
-538846106
1273642419
1935803228
-1359637234
996406378
This sample is the output observed in the tested environment. The fixed seed makes the engine’s starting state repeatable, but exact distribution values can differ across standard-library implementations because distribution algorithms are not required to produce an identical sequence everywhere. For an application that needs nondeterministic seeding, seed the engine from an appropriate source; avoid constructing a new engine for each draw.
Use integer limits safely
A limit macro is useful when validating a value or describing an allowed range, but checking the result after an overflowing operation is too late. For addition of nonnegative int values, check a <= INT_MAX - b before evaluating a + b. For subtraction, reason about the lower and upper bounds before calculating; negating INT_MIN is not representable as an int on implementations where the range is asymmetric. Signed overflow has undefined behavior in C++, so do not rely on it wrapping to another value.
If a calculation can exceed the int range, choose a type with sufficient range or use checked arithmetic before performing the operation. The wider type must itself be large enough for the calculation. INT_MIN and INT_MAX are useful boundary values for tests and guards, but they cannot prevent overflow unless the surrounding logic checks inputs before arithmetic.