C++ Int Maximum Value

In C++, an int is one of the most commonly used data types to store integer values. The size of an int is typically 32 bits on modern systems, and the maximum value it can represent is 2,147,483,647. This value is defined by the INT_MAX macro in the <climits> header.


Maximum Limit of Int Data Type

The int data type represents numbers in the range:

  • Minimum Value: -2,147,483,648
  • Maximum Value: 2,147,483,647

The range is derived from the formula:

-2^(n-1) to 2^(n-1) - 1

Where n is the number of bits used by the data type. For an int, n = 32, resulting in:

-2^(32-1) to 2^(32-1) - 1 = -2,147,483,648 to 2,147,483,647

From this, the Maximum Limit of int data type is 2147483647.


C++ Program to Access Int Maximum Value

You can programmatically access the maximum value of an int using the INT_MAX constant from the <climits> header.

The following example demonstrates how to access and use the maximum value of an int in your programs.

main.cpp

</>
Copy
#include <iostream>
#include <climits>

int main() {
    // Accessing the maximum value of int
    std::cout << "The maximum value of int is: " << INT_MAX << std::endl;
    return 0;
}

Output

The maximum value of int is: 2147483647

Explanation

  • The <climits> header provides macros for the limits of fundamental data types in C++.
  • The INT_MAX macro defines the maximum value of an int, which is 2,147,483,647 for a 32-bit integer.
  • The program uses std::cout to output the maximum value of an int directly using INT_MAX.