Why do we need these macros?
Definite algorithms may require variables to be initialized with minimum / maximum values. The bits in the data type always depend on the compiler.
The reasons behind why there is a need for these types of macros:
Don’t have to remember the original value. Have a unified programming configuration on all machines. Very easy to practice.
Use of INT_MAX
INT_MAX is a key code defined to obtain the largest value for items. We see how to use the INT_MAX in C++ to get the maximum number. The original values are determined by the library execution. The INT_MAX macro is elucidated in both header files <limits.h> and <climits>, so we can utilize the library #include <limits.h> instead of the #include <climits> library. In this instance <stdio.h> header file defines input-output. Besides, in the body of the main function, we apply the ‘printf’ function. We utilize this function to print the output. We obtain the maximum value by passing the INT_MAX as an argument to this function.
#include <stdio.h>
int main()
{
printf("%d\n", INT_MAX);
}
The value of INT_MAX can fluctuate from compiler to compiler. Its value differs between 32-bit compiler and 64-bit compilers.
‘2147483647’ is the maximum value in a 32-bit compiler.
Check for Integer overflow
We can check the integer overflow by adding two integral numbers. Here we integrate a new header file. #include <bits/stdc++.h> that is used to define input-output function. The second library #include <limits.h> always defines INT_MAX key code. Next, we declare the function to check the overflow of the integer. We apply the if-else condition to check if adding two integers will cause overflow or not.
#include <limits.h>
int check_overflow(int x, int y)
{
if (x > INT_MAX - y)
return -1;
else
return x + y;
}
int main()
{
int x = 2147483627;
int y = 30;
int r = check_overflow(x, y);
if (r == -1)
std::cout << "Overflow occurred";
else
std::cout << r;
}
Moreover, we declare two variables with integer data types and assign them values. The overflow will happen in the code when any one variable is incremented. If the overflow has occurred, it returns -1. And saves the output in a new variable. If it happens, the std::cout function prints the message ‘overflow occurred’ otherwise, it returns the resultant value.
After running the above code, the integer overflow occurred, so we get the text in the form of output.
Use for loop
To obtain the maximum number in an array, we can utilize the “for” loop with a variable containing the maximum value found in the array. The array has all integral numbers.
#include <cmath>
#include <climits>
using namespace std;
int main (void)
{
int k = 0;
int iMaths [ 5 ];
int sum = 0;
float ave = 0;
int m;
for (k = 1 ; k < 6 ; k++)
{
cout << "Enter number " << k <> iMaths [ k ];
if (iMaths [k] > m)
{
m = iMaths [k];
}
}
for ( k =1 ; k < 6 ; k++)
{
cout << "Numbers entered " << k << " = " << iMaths [k] << endl;
sum = sum + iMaths[k];
ave = sum/10;
}
cout << "Sum = " << sum << endl;
cout << "Average = " << ave << endl;
cout << "The largest number entered = " << m << endl;
return 0;
}
At the start of this program, we include three different header files. The library <iostream> is included for input-output purpose. <cmath> is used for mathematical calculations. Further, we integrate the third header file <climits> to define the INT_MAX. Next, we initialize the variables. All the variables have integer data types except one. The ‘ave’ variable that stores the mean value of an entered array has a float data type.
We write a code that requests the user to enter any 5 numbers in the array. We use for loop in this code. We see if the succeeding value in the array is greater than the preceding specified maximum in every loop. If it is included, fix it to that value and repeat the process. When the loop finishes, the variable contains the largest number of the array. As soon as the numbers are entered, we apply a function that determines the sum, the largest number of the array, and the mean value of the entered numbers.
In the output, we get the list of 5 entered numbers. Similarly, we obtain sum, average and maximum value.
Conclusion
This article describes INT_MAX. C ++ has a specific key code representing the maximum number and can be allocated to variables without entering an integer. INT_MAX constant is a key code that is defined in the library <climits>. We used this to acquire the highest number of an integral object, and it returns the largest number that an object can save. By using INT_MAX, we checked the integer overflow. In the end, we utilized for loop to find the highest number.