INT_MAX and INT_MIN in C/C++
In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact number. As a result, C++ provides specific macros to represent these numbers so they can be assigned to variables directly without typing the entire amount.
You should include the header file limits.h or limitations in your C or C++ source code, depending on the compiler and the C++ standard. To use the INT MAX and INT MIN macros, it is recommended to include this header file. Starting a variable as the lowest/highest value for specific algorithms is frequently essential.
Depending on the computer, the datatype's bit count may change.
It would be convenient if everyone could utilize the exact macros to ensure consistency in using the maximum/minimum values!
This is the reason why these macros are available.
- To save you from having to recall the actual values.
- Use the same programming techniques across all machines.
- Extremely practical to utilize
These arguments should persuade you to employ these macros when creating your own C/C++ library.
Note: If we serialize a binary search tree using in-order traversal, we can obtain a list of data in ascending order. The definition of BST can be used to demonstrate it (Binary search tree). In this instance, I mark the address of the previous node in the list using the reference of the Tree Node Pointer prior as a global variable.
A macro named INT MAX states that an integer variable cannot store any value higher than this cap.
An integer variable cannot hold any value lower than what is specified by the INT MIN flag.
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.
- The value of INT_MAX is +2147483647.
- The value of INT_MIN is -2147483648.
Example:
// A C++ program to output INT MAX values
// both, INT MIN
#include <bits/stdc++.h>
usingnamespace std;
intmain()
{
cout<< INT_MAX <<endl;
cout<< INT_MIN;
return 0;
}
Output:
Properties of INT_MAX are:
- It can store positive and negative numbers because it is a signed data type.
- Takes up 32 bits, of which 1 bit is utilized to store the integer's sign.
- The most significant integer value typically stored in an int data type is 2, 147, 483, 647, or roughly 231 - 1, but this depends on the compiler.
- The climits> header file contains a constant with INT MAX representing the maximum value that may be stored in an int.
INT MAX and INT MIN Applications
1. Verify for integer overflow first:
// Using C++, check for integer overflow
// merging two numbers
#include <bits/stdc++.h>
// Checking function for integer overflow
int check_overflow(int num1, int num2)
{
// to see if the addition will result in an overflow
if (num1 > INT_MAX - num2)
return -1;
// No overflow took place.
else
return num1 + num2;
}
int main()
{
// These integers will add up to INT MAX.
//Overflow occurs if any of them are increased by one.
// going to happen
int num1 = 2147483627;
int num2 = 20;
// Result if Overflow occurred is -1.
// keeps the amount; otherwise.
int result = check_overflow(num1, num2);
//Overflow happened
if (result == -1)
std::cout<< "Integer overflow occurred";
// no spillover
else
std::cout<< result;
}
Output:
Similarly, we may use INT MIN to check for Overflow when subtracting two values.
2. MIN computation in a considerable element array
We often give MIN a large number to compute the smallest value in an array. However, we must provide the collection with the highest value if an array contains several significant elements.
Example:
// MIN element computation in C++
#include <bits/stdc++.h>
// Function to determine the array's minimal element
intcompute_min(intarr[], int n)
{
// placing the highest value
int MIN = INT_MAX;
// moving through and updating MIN
for (inti = 0; i< n; i++)
MIN = std::min(MIN, arr[i]);
// MIN element printing
std::cout<< MIN;
}
intmain()
{
// array with the MIN computation
intarr[] = { 2019403813, 2147389580, 2145837140,
2108938594, 2112076334 };
// arraysize
int n = sizeof(arr) / sizeof(arr[0]);
// Calling the MIN function
compute_min(arr, n);
}
Output:
Using INT MIN, MAX can also be in an array of tremendous values.