INT_MAX and INT_MIN Macro Expressions in C++
-
Use
INT_MIN
andINT_MAX
to Access Type Specific Limits in C++ -
Use
INT_MIN
andINT_MAX
to Generate Random Numbers in C++ - Conclusion
- FAQ

When programming in C++, understanding the limits of integer types is crucial for avoiding overflow errors and ensuring your applications run smoothly. Two essential macro expressions, INT_MAX
and INT_MIN
, are defined in the <limits.h>
header file and help you determine the maximum and minimum values that an int
can hold.
This article will guide you through the utilization of these macros, providing practical examples and explaining their significance in real-world applications. Whether you are a beginner or an experienced developer, grasping these concepts will enhance your coding skills and improve the reliability of your software.
Use INT_MIN
and INT_MAX
to Access Type Specific Limits in C++
C++ language defines multiple built-in data types with some specifications about how much memory they should occupy and the corresponding maximum/minimum values. Data types like integers are commonly used in calculations where their possible maximum and minimum values need to be considered for the problem. Although the limits are dependent on the specific types’ storage size, these vary based on the hardware platform. Thus, we need to access these values with a fixed handle, hence the macro expressions - INT_MIN
and INT_MAX
. These correspond to the minimum and maximum values of the signed int
data type. The following example demonstrates multiple macro expressions that are available under the <climits>
header.
#include <climits>
#include <iostream>
int main() {
printf("CHAR_BIT = %d\n", CHAR_BIT);
printf("MB_LEN_MAX = %d\n", MB_LEN_MAX);
printf("\n");
printf("CHAR_MIN = %+d\n", CHAR_MIN);
printf("CHAR_MAX = %+d\n", CHAR_MAX);
printf("SCHAR_MIN = %+d\n", SCHAR_MIN);
printf("SCHAR_MAX = %+d\n", SCHAR_MAX);
printf("UCHAR_MAX = %u\n", UCHAR_MAX);
printf("\n");
printf("SHRT_MIN = %+d\n", SHRT_MIN);
printf("SHRT_MAX = %+d\n", SHRT_MAX);
printf("USHRT_MAX = %u\n", USHRT_MAX);
printf("\n");
printf("INT_MIN = %+d\n", INT_MIN);
printf("INT_MAX = %+d\n", INT_MAX);
printf("UINT_MAX = %u\n", UINT_MAX);
printf("\n");
printf("LONG_MIN = %+ld\n", LONG_MIN);
printf("LONG_MAX = %+ld\n", LONG_MAX);
printf("ULONG_MAX = %lu\n", ULONG_MAX);
printf("\n");
printf("LLONG_MIN = %+lld\n", LLONG_MIN);
printf("LLONG_MAX = %+lld\n", LLONG_MAX);
printf("ULLONG_MAX = %llu\n", ULLONG_MAX);
printf("\n");
return EXIT_SUCCESS;
}
Output:
CHAR_BIT = 8
MB_LEN_MAX = 16
CHAR_MIN = -128
CHAR_MAX = +127
SCHAR_MIN = -128
SCHAR_MAX = +127
UCHAR_MAX = 255
SHRT_MIN = -32768
SHRT_MAX = +32767
USHRT_MAX = 65535
INT_MIN = -2147483648
INT_MAX = +2147483647
UINT_MAX = 4294967295
LONG_MIN = -9223372036854775808
LONG_MAX = +9223372036854775807
ULONG_MAX = 18446744073709551615
LLONG_MIN = -9223372036854775808
LLONG_MAX = +9223372036854775807
ULLONG_MAX = 18446744073709551615
Use INT_MIN
and INT_MAX
to Generate Random Numbers in C++
Random numbers are often generated between some fixed range. It’s useful to have number data type limits to specify the range parameters accordingly and not overflow the generated results. The following example code utilizes the uniform_int_distribution
to limit the range of the generated numbers. In this case, we specified the INT_MAX
and INT_MIN
values to retrieve the integers from the maximum available range.
#include <climits>
#include <iomanip>
#include <iostream>
#include <random>
using std::cout;
using std::endl;
using std::setprecision;
int main() {
std::random_device rd;
std::default_random_engine eng(rd());
std::uniform_int_distribution<int> distr(INT_MIN, INT_MAX);
for (int n = 0; n < 6; ++n) {
cout << distr(eng) << "\n";
}
cout << endl;
return EXIT_SUCCESS;
}
-895078088
1662821310
-636757160
1830410618
1031005518
-438660615
Conclusion
In conclusion, INT_MAX
and INT_MIN
are invaluable tools in C++ programming that help you manage integer limits effectively. By understanding how to use these macros, you can prevent overflow errors and enhance the reliability of your applications. Whether you are checking for overflow conditions or implementing algorithms, incorporating INT_MAX
and INT_MIN
into your code will undoubtedly improve its robustness. As you continue to develop your skills in C++, remember the significance of these constants and apply them in your projects for better results.
FAQ
-
What is the purpose of INT_MAX and INT_MIN in C++?
INT_MAX and INT_MIN are used to define the maximum and minimum values that an int can hold, helping to prevent overflow errors. -
How do I include INT_MAX and INT_MIN in my C++ program?
You can include these macros by adding#include <limits.h>
at the beginning of your C++ program. -
Can I use INT_MAX and INT_MIN with other data types?
While INT_MAX and INT_MIN are specific to the int data type, similar macros exist for other types, such as LONG_MAX and LONG_MIN for long integers. -
What happens if I exceed INT_MAX in my calculations?
Exceeding INT_MAX can lead to overflow, resulting in undefined behavior or incorrect values in your calculations. -
Are INT_MAX and INT_MIN platform-dependent?
The values of INT_MAX and INT_MIN are defined by the C++ standard but can vary depending on the system architecture and compiler.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook