×

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

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

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 Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Data Hiding in C++

C++ : High-performance apps can be made using the cross-platform language C++. Bjarne Stroustrup created C++ as an addition to the C language. Programmers have extensive control over memory and system...

3 minutes read.

Sizeof() Operators in C++

sizeof() Operators in C++ The sizeof() operator in C++ defines the size of variables, constants, or data types. It is a unique operator that manipulates other operators and returns the size...

4 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

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.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

Decimal to Octal in C++

We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of...

3 minutes read.

C++ Range-based For Loop

In C++ language, the range-based for loop was added, which is far superior than the ordinary For loop. The implementation of a range-based for loop doesn't really need much code. It's a...

4 minutes read.