Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

long is a fundamental, signed integral data type in C++ guaranteed by the standard to be at least 32 bits in width. It is designed to represent whole numbers, though its exact memory footprint is compiler- and architecture-dependent based on the underlying data model.

Memory Size and Data Models

Unlike fixed-width integer types (e.g., int32_t, int64_t), the size of long varies across platforms:
  • Standard Guarantee: >= 32 bits.
  • LLP64 Data Model (64-bit Windows): long is 32 bits (same size as int).
  • LP64 Data Model (64-bit Linux/macOS): long is 64 bits.
  • ILP32 Data Model (32-bit systems): long is 32 bits.

Syntax and Aliases

The long keyword can be used alone or combined with int and signedness modifiers. By default, long is signed.
// The following declarations are strictly equivalent:
long val1 = 100;
long int val2 = 100;
signed long val3 = 100;
signed long int val4 = 100;

// Unsigned variant (guaranteed >= 32 bits, strictly non-negative):
unsigned long uval1 = 200;
unsigned long int uval2 = 200;

Literals

To explicitly type an integer literal as long, append the L or l suffix. For unsigned long, use UL, Ul, uL, or ul. Uppercase is standard practice to avoid visual confusion between the lowercase letter l and the number 1.
auto a = 2147483647L;  // Type deduced as long
auto b = 4294967295UL; // Type deduced as unsigned long

Value Ranges

The minimum and maximum representable values depend on the platform’s bit width. These boundaries can be queried programmatically using the <limits> header.
#include <iostream>
#include <limits>

int main() {
    // Minimum value: typically -2147483648 (32-bit) or -9223372036854775808 (64-bit)
    std::cout << std::numeric_limits<long>::min() << '\n';

    // Maximum value: typically 2147483647 (32-bit) or 9223372036854775807 (64-bit)
    std::cout << std::numeric_limits<long>::max() << '\n';

    // Unsigned maximum: typically 4294967295 (32-bit) or 18446744073709551615 (64-bit)
    std::cout << std::numeric_limits<unsigned long>::max() << '\n';

    return 0;
}

Type Conversion

In expressions involving mixed integral types, C++ applies the usual arithmetic conversions based on integer conversion rank. The C++ standard dictates that the rank of long is strictly greater than the rank of int, regardless of their actual bit widths on the target architecture. Consequently, if a binary operation involves an int and a long, the int operand always undergoes an implicit integral conversion to long before the expression is evaluated. In strict C++ terminology, this is an integral conversion, not an integral promotion (which applies exclusively to converting types smaller than int, such as short or char, up to int).
int x = 10;
long y = 20L;

// 'x' undergoes integral conversion to long. 
// The resulting type of 'z' is long.
auto z = x + y; 
Master C++ with Deep Grasping Methodology!Learn More