Most of the times, 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.

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.

`// C++ program to print values of INT_MAX ` `// and INT_MIN ` `#include<bits/stdc++.h> ` `int` `main() ` `{ ` ` ` `printf` `(` `"%d\n"` `, INT_MAX); ` ` ` `printf` `(` `"%d"` `, INT_MIN); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

2147483647 -2147483648

**Applications of INT_MAX and INT_MIN :**

1. Check for Integer overflow :

`// C++ code to check for Integer overflow while ` `// adding 2 numbers ` `#include <bits/stdc++.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 occured ` ` ` `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` ` ` `std::cout << result; ` `} ` |

*chevron_right*

*filter_none*

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 :

`// C++ code to compute MIN element ` `#include <bits/stdc++.h> ` ` ` `// 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 ` ` ` `std::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); ` `} ` |

*chevron_right*

*filter_none*

Output:

2019403813

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

This article is contributed by **Rohit Thapliyal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- C++ string class and its applications
- C++ String Class and its Applications | Set 2
- stringstream in C++ and its applications
- strchr() function in C++ and its applications
- Reference to a pointer in C++ with examples and applications
- MakeFile in C++ and its applications
- Applications of Pointers in C/C++
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- fesetround() and fegetround() in C++ and their application
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Get first and last elements from Array and Vector in CPP
- Similarities and Differences between Ruby and C++
- Generate an array of given size with equal count and sum of odd and even numbers
- Similarities and Difference between Java and C++
- Comma in C and C++
- How does "void *" differ in C and C++?
- What are the operators that can be and cannot be overloaded in C++?
- Data type of character constants in C and C++
- Can we use function on left side of an expression in C and C++?
- delete and free() in C++