The Wayback Machine - https://web.archive.org/web/20230324040027/https://www.geeksforgeeks.org/int_max-int_min-cc-applications/
Skip to content
Related Articles
Open in App
Not now

Related Articles

INT_MAX and INT_MIN in C/C++ and Applications

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 23 Feb, 2023
Improve Article
Save Article

Most of the time, in competitive programming, there is a need to assign the variable, the maximum or minimum value that data type can hold, but remembering such a large and precise number comes out to be a difficult job. Therefore, C++ has certain macros to represent these numbers, so that these can be directly assigned to the variable without actually typing the whole number.

Depending upon the compiler and C++ standard, you may be required to include the header file <limits.h> or <climits> in your C or C++ source code respectively. So it is advisable to include this header file for using the INT_MAX, INT_MIN macros. For further reading on this header file, refer to this article. 

NOTE :- If we use in-order traversal to serialize a binary search tree, we can get a list of values in ascending order. It can be proved with the definition of BST(Binary search tree). And here I use the reference of Tree Node pointer prev  as a global variable to mark the address of previous node in the list.

INT_MAX is a macro that specifies that an integer variable cannot store any value beyond this limit. 
INT_MIN specifies that an integer variable cannot store any value below this limit.

Values of INT_MAX and INT_MIN may vary
from compiler to compiler. Following are
typical values in a compiler where integers
are stored using 32 bits.

Value of INT_MAX is +2147483647.
Value of INT_MIN is -2147483648.

CPP




// C++ program to print values of INT_MAX
// and INT_MIN
#include <bits/stdc++.h>
using namespace std;
int main()
{
    cout << INT_MAX << endl;
    cout << INT_MIN;
    return 0;
}

C




// 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

2147483647
-2147483648

Applications of INT_MAX and INT_MIN : 

1. Check for Integer overflow : 

CPP




// C++ code to check for Integer overflow while
// adding 2 numbers
#include <bits/stdc++.h>
using namespace std;
// 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;
 
    // No overflow occurred
    else
        return num1 + num2;
}
 
// Driver code
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);
 
    // Overflow occurred
    if (result == -1)
        std::cout << "Integer overflow occurred";
 
    // No overflow
    else
        cout << result;
}

C




// C code to check for Integer overflow while
// adding 2 numbers
 
#include <stdio.h>
#include <limits.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;
 
    // No overflow occurred
    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
    // Stores the sum, otherwise
    int result = check_overflow(num1, num2);
 
    // Overflow occurred
    if (result == -1)
        printf("Integer overflow occurred");
 
    // No overflow
    else
        printf("%d", result);
}
 
// This code is contributed by sarajadhav12052009

Output

2147483647

Similarly, we can check for overflow while subtracting 2 numbers using INT_MIN.

2. Computing MIN in an array with large elements 
We usually assign a high value to MIN to compute minimum value in an array. But if an array has large elements, we must assign the highest possible value to the array.

Below is the C++ implementation : 

CPP




// C++ code to compute MIN element
#include <bits/stdc++.h>
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 = std::min(MIN, arr[i]);
 
    // Printing MIN element
    cout << MIN;
}
 
// Driver code
int main()
{
    // array with MIN to compute
    int arr[] = { 2019403813, 2147389580, 2145837140,
                  2108938594, 2112076334 };
 
    // size of array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to compute MIN
    compute_min(arr, n);
}

Output

2019403813

Similarly, MAX can be found in an array of large numbers using INT_MIN.

Why abs(INT_MIN) does not give expected result?

Have you ever faced a problem when you might have used abs() function, Most Probably NO if you have not solved any problem which requires absolute function. But if you have solved problems on GeeksforGeeks or Leetcode then you know that there is always one testcase where you fails and that test case is when you have the value as INT_MIN.

Lets see what happens if we use absolute function it returns the mod value that means it returns following value:

Modulus Values

Now another thing that we know is the range of integer is from -2,147,483,648 to 2,147,483,647 or we can say that it is from -231 to 231 – 1 so as we can see that there is always one more on negative side than positive one

Now lets see what happens when we try to take absolute values of the result:

C++




#include <bits/stdc++.h>
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;
}

Output

Value Of INT_MIN is : -2147483648
Value Of abs(INT_MIN) is : -2147483648

Now we can observe that abs(INT_MIN) is INT_MIN itself, and this causes many error to occur when we give any online assessments or solve any problem.

Reason:

Now if we come to the reason part we can see that we are currently dealing with the Integer part and abs(integer) returns an integer value itself, so moving next to representation INT_MIN can be represented as

INT_MIN = -2147483648 = 11000000000000000000000000000000

Here 1st bit represents the sign bit which is set to one meaning it is an negative number and the next part is a 31bit binary representation for 2147483648.

Now if we try to take absolute value of INT_MIN it will try to give us +2147483648 and this value can’t be represented in the Integer form as the maximum value that can be represented is +2147483647 as on the positive side we have to represent 231 integers but 0 is also included so the range from 1 to 2147483648 changes to 0 to 2147483647 and for this reason the abs(INT_MIN) can’t be represented in this range and the answer returned is same as INT_MIN.

Solution:

Well, there might be many solution to the problem but some of the best solutions are:

Always use a special edge case for checking if(x == INT_MIN) if you are using abs(x) and handle this case accordingly.
Try to use Long instead of INTEGER but remember LONG_MIN will also give the same result so be careful

This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!