C++ Long Minimum Value

In C++, a long is an integer data type that can store larger integer values compared to an int. The size of a long is typically 32 bits on most systems, but it can be 64 bits on some systems. The minimum value a long can represent is -2,147,483,648 for a 32-bit system or -9,223,372,036,854,775,808 for a 64-bit system. This value is defined by the LONG_MIN macro in the <climits> header.


Minimum Limit of Long Data Type

The long data type represents numbers in the range:

  • Minimum Value: -2,147,483,648 (32-bit systems) or -9,223,372,036,854,775,808 (64-bit systems)
  • Maximum Value: 2,147,483,647 (32-bit systems) or 9,223,372,036,854,775,807 (64-bit systems)

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 a long:

  • For 32-bit systems: n = 32, resulting in -2,147,483,648 to 2,147,483,647.
  • For 64-bit systems: n = 64, resulting in -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

C++ Program to Access Long Minimum Value

You can programmatically access the minimum value of a long using the LONG_MIN constant from the <climits> header.

In the following example, we demonstrate how to access and use the minimum value of a long.

main.cpp

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

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

Output in a 64-bit system

The minimum value of long is: -9223372036854775808

Output in a 32-bit system

The minimum value of long is: -2147483648

Explanation

  • The <climits> header provides macros for the limits of fundamental data types in C++.
  • The LONG_MIN macro defines the minimum value of a long. Its value depends on whether the system uses a 32-bit or 64-bit architecture.
  • The program uses std::cout to output the minimum value of a long directly using LONG_MIN.