Open In App

INT_MAX and INT_MIN in C/C++ and Applications

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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/C++ has certain macros to represent these numbers, so that these can be directly assigned to the variable without actually typing the whole number.

C/C++ provides two such macros namely INT_MAX and INT_MIN that represents the integer limits. 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, and INT_MIN macros. For further reading on this header file, refer to this article.

INT_MAX in C/C++

INT_MAX is a macro that specifies that an integer variable cannot store any value beyond this limit. It represents the maximum value of the upper limit of the integer data type in C/C++. 

The value of INT_MAX is:

  • INT_MAX = 2147483647   (for 32-bit Integers)
  • INT_MAX = 9,223,372,036,854,775,807   (for 64-bit Integers)

INT_MIN in C/C++

INT_MIN is a macro that specifies that an integer variable cannot store any value below this limit. It represents the minimum value or the upper limit of the integer data type.

The value of INT_MIN is:

  • INT_MIN = –2147483648   (for 32-bit Integers)
  • INT_MIN = –9,223,372,036,854,775,808   (for 64-bit Integers)

Note: 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.

Example of INT_MIN and INT_MAX

C++




// C++ program to print values of INT_MAX
// and INT_MIN
#include <iostream>
#include <limits.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

Following are the major applications of INT_MAX and INT_MIN

1. Check for Integer Overflow

We can use the INT_MIN and INT_MAX macros to check for the signed integer overflow. The example below demonstrate how to do it.

Example

C++




// C++ code to check for Integer overflow while
// adding 2 numbers
#include <climits>
#include <iostream>
using namespace std;
 
// Function to return integer sum after checking 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)
        cout << "Integer overflow occurred";
 
    // No overflow
    else
        cout << result;
 
    return 0;
}


C




// C code to check for Integer overflow while
// adding 2 numbers
#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;
 
    // 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);
 
    return 0;
}
 
// 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 the 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 implementation:

Example

C++




// C++ code to compute MIN element
#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 = 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.

FAQs on INT_MIN and INT_MAX

1. Why abs(INT_MIN) does not give the 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 an absolute function. But if you have solved problems on GeeksforGeeks or Leetcode then you know that there is always one test case where you fails and that test case is when you have the value as INT_MIN.

Let’s see what happens if we use the absolute function it returns the mod value which means it returns the 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 the negative side than the positive one

Now let’s see what happens when we try to take absolute values of the result:

C++




// C++ program to demonstrate the common error faced when
// getting absolute value of the INT_MIN
#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




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

Now we can observe that abs(INT_MIN) is INT_MIN itself, and this causes many errors 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 = 10000000000000000000000000000000

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

Now if we try to take the 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 solutions to the problem but some of the best solutions are:

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


Last Updated : 07 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads