×

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:

INT_MAX and INT_MIN in C/C++

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:

INT_MAX and INT_MIN in C/C++

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:

INT_MAX and INT_MIN in C/C++

Using INT MIN, MAX can also be in an array of tremendous values.


Related Topics

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

Sum of all elements between k1’th and k2’th Smallest Elements

In this tutorial, we will look at how to determine sum of all given elements between two given indexes’ smallest elements. Assuming an array of integers and two numbers, k1...

2 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

Compile Time Polymorphism in C++

What is Polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in...

4 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

C++ Exception Handling

Exception is an unexpected problem that occurs at program run time. This problem might include condition such as division by zero, running out of memory space, array out of bonds, etc....

2 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

C++ Enumeration

C++ Enumeration In C++, Enum is a special data type that contains some fixed sets of components that have various applications in the programming. Enum works fine with fixed constant sets...

3 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.