INT_MAX and INT_MIN in C/C++ and Applications
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
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.
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:
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:
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:
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.
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 = 11111111111111111111111111111111
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.
Well, there might be many solutions 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.
Please Login to comment...