CKTIS / C++
INT_MAX — the largest int value
The INT_MAX macro from <climits> specifies the largest int value for an implementation. Use it instead of hard-coding the limit, and check it before incrementing a value to avoid overflow.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <climits>
INT_MAXINT_MAX is a macro from <climits> whose value is the largest number representable by int. It expands to a constant expression of type int; it is neither a variable nor a member of namespace std. Write INT_MAX, not std::INT_MAX.
The exact value depends on the implementation. C++ guarantees at least 32767, but does not guarantee that int is exactly 32 bits wide. For a 32-bit int, the limit is 2147483647. Using INT_MAX instead of that literal lets code adapt to the type's actual range.
INT_MAX does not automatically protect against overflow. Evaluating INT_MAX + 1 as an int operation exceeds the range; signed arithmetic overflow has undefined behavior. Check value < INT_MAX before executing ++value, not afterward. This guards an increment, rather than providing a general safety test for arbitrary addition.
The example starts at INT_MAX - 1, safely reaches the limit, and blocks another increment. It prints a comparison result instead of the numeric limit, so its expected output does not depend on the width of int. The sources show the evolving standard draft rather than a frozen C++23 publication; the properties described here also apply to C++23.
Examples
#include <climits>
#include <iostream>
int main() {
int value = INT_MAX - 1;
if (value < INT_MAX) {
++value;
}
std::cout << std::boolalpha << (value == INT_MAX) << '\n';
if (value == INT_MAX) {
std::cout << "increment blocked\n";
}
}
Result
true increment blocked
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.