INT_MAX and INT_MIN are predefined macros that define the valid range of the int data type. They are widely used for overflow checking, boundary conditions, and algorithm initialization.
- Used to represent integer boundary values in programs.
- Defined in <limits.h> (C) and <climits> (C++).
Example: C++ program to print values of INT_MAX and INT_MIN
#include <iostream>
#include <climits>
using namespace std;
int main()
{
cout << "INT_MAX = " << INT_MAX << endl;
cout << "INT_MIN = " << INT_MIN;
return 0;
}
// C program to print values of INT_MAX
// and INT_MIN
// we have to include limits.h for results in C
#include <limits.h>
#include <stdio.h>
int main()
{
printf("%d\n", INT_MAX);
printf("%d", INT_MIN);
}
Output
INT_MAX = 2147483647 INT_MIN = -2147483648
Header Files
#include <limits.h> // C
or
#include <climits> // C++
Value of INT_MAX
INT_MAX represents the largest value that can be stored in an int. Typical values are:
| Integer Size | INT_MAX |
|---|---|
| 32-bit | 2147483647 |
| 64-bit* | 9223372036854775807 |
Note: On most modern systems, int is still 32 bits. The 64-bit value usually corresponds to LONG_LONG_MAX, not INT_MAX. The actual value depends on the compiler and platform.
Value of INT_MIN
INT_MIN represents the smallest value that can be stored in an int. Typical values are:
| Integer Size | INT_MIN |
|---|---|
| 32-bit | -2147483648 |
| 64-bit* | -9223372036854775808 |
Note: These values may vary depending on the implementation.
Applications of INT_MAX and INT_MIN
These macros are commonly used in the following scenarios:
- Detecting integer overflow and underflow.
- Initializing variables while finding minimum or maximum values.
- Defining boundary values in algorithms.
Detecting Integer Overflow
Before performing arithmetic operations, INT_MAX and INT_MIN can be used to verify whether the result would exceed the valid integer range.
#include <climits>
#include <iostream>
using namespace std;
int check_overflow(int num1, int num2)
{
// Checking if addition will cause overflow
if (num1 > INT_MAX - num2)
return -1;
else
return num1 + num2;
}
int main()
{
// The sum of these numbers will equal INT_MAX
// If any of them is incremented by 1, overflow
// will occur
int num1 = 2147483627;
int num2 = 20;
// Result is -1 if overflow occurred
// Stores the sum, otherwise
int result = check_overflow(num1, num2);
if (result == -1)
cout << "Integer overflow occurred";
else
cout << result;
return 0;
}
#include <limits.h>
#include <stdio.h>
// Function to check integer overflow
int check_overflow(int num1, int num2)
{
// Checking if addition will cause overflow
if (num1 > INT_MAX - num2)
return -1;
else
return num1 + num2;
}
int main(void)
{
// The sum of these numbers will be equivalent to
// INT_MAX If any of them is incremented by 1, overflow
// will occur
int num1 = 2147483627;
int num2 = 20;
// Result is -1 if overflow occurred
// print the sum, otherwise
int result = check_overflow(num1, num2);
if (result == -1)
printf("Integer overflow occurred");
else
printf("%d", result);
return 0;
}
Output
2147483647
Explanation
- INT_MAX is used to verify whether the addition exceeds the maximum integer value.
- Similar checks using INT_MIN can detect underflow during subtraction.
Finding Minimum and Maximum Elements
INT_MAX is commonly used to initialize the minimum value, while INT_MIN is used to initialize the maximum value.
#include <climits>
#include <iostream>
using namespace std;
// Function to compute minimum element in array
int compute_min(int arr[], int n)
{
// Assigning highest value
int MIN = INT_MAX;
// Traversing and updating MIN
for (int i = 0; i < n; i++)
MIN = min(MIN, arr[i]);
cout << "Minimum Element of : "<<MIN<<endl;
}
// Function to compute maximum element in array
int compute_max(int arr[], int n)
{
// Assigning highest value
int MAX = INT_MIN;
// Traversing and updating MIN
for (int i = 0; i < n; i++)
MAX = max(MAX, arr[i]);
cout << "Maximum Element: "<<MAX<<endl;
}
int main()
{
int arr[] = { 2019403813, -214738958, 2145837140, -210893859, 2112076334};
int n = sizeof(arr) / sizeof(arr[0]);
compute_min(arr, n);
compute_max(arr, n);
}
Output
2019403813
Explanation
- Initialize the minimum variable with INT_MAX.
- Initialize the maximum variable with INT_MIN.
- Update these values while traversing the array.
Note: Your current output is incorrect. It should display both the minimum and maximum values, not just 2019403813.
Behavior of abs(INT_MIN)
The abs() function normally returns the absolute (non-negative) value of an integer. The following illustration shows how the function behaves for ordinary integer values.
.png)
However, INT_MIN is a special case because its absolute value cannot be represented by the int data type.
Example
#include <climits>
#include <iostream>
using namespace std;
int main()
{
cout << "Value of INT_MIN is: " << INT_MIN << endl;
cout << "Value of abs(INT_MIN) is: " << abs(INT_MIN)
<< endl;
return 0;
}
// C program to demonstrate the common error faced when
// getting absolute value of the INT_MIN
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Value of INT_MIN is: %d\n", INT_MIN);
printf("Value of abs(INT_MIN) is: %d", abs(INT_MIN));
return 0;
}
Output
Value of INT_MIN is: -2147483648 Value of abs(INT_MIN) is: -2147483648
Explanation
- You can shorten it to:
- A 32-bit signed int stores values from -2,147,483,648 to 2,147,483,647.
- The absolute value of INT_MIN would be 2,147,483,648, which exceeds INT_MAX.
- Since this value cannot be represented by the int type, abs(INT_MIN) typically returns INT_MIN.
Handling INT_MIN
To safely use abs() with integers:
- Check whether the value is INT_MIN before calling abs().
- Use a wider integer type such as long long when appropriate.
- Remember that LONG_MIN has the same limitation when used with labs().