long long

What Is a Long Integer?

A long integer is a whole-number type declared with the keyword long. Learn its minimum sizes, how the L suffix works and when to use long long.

On this page
  1. The integer types in order
  2. Checking the size of long
  3. Declaring long values
  4. When long is not enough
  5. Signed and unsigned long
  6. Which type to use

A long integer is a whole-number type declared with the keyword long. In C++, long and long int name the same type. Among the primitive data types in C, long is one of the integer types, and the standard requires it to hold at least 32 bits. That means it can store whole numbers up to about 2.1 billion on every system. On many 64-bit systems it is wider, so the exact size depends on the platform.

The integer types in order

The main integer types, from narrowest to widest, are short, int, long and long long. Each type can hold at least as large a range as the one before it.

Type Guaranteed minimum width
short 16 bits
int 16 bits
long 32 bits
long long 64 bits

These are minimums. Most desktop systems use 32 bits for int, and the width of long varies by platform.

You may wonder: is int 2 or 4 bytes in C? The standard allows 16 bits, which is 2 bytes, but 4 bytes is the common size on desktop systems. So a long is not automatically bigger than an int.

Checking the size of long

#include <iostream>

int main() {
    std::cout << "int:       " << sizeof(int) << " bytes\n";
    std::cout << "long:      " << sizeof(long) << " bytes\n";
    std::cout << "long long: " << sizeof(long long) << " bytes\n";
    return 0;
}

On 64-bit Linux with GCC or Clang, this often prints 4, 8 and 8 bytes. On 64-bit Windows, the long line usually shows 4 bytes. Run it on your own machine to see the real numbers. The sizeof function in C explains how the operator works.

Declaring long values

Declare a long like any other variable. The L suffix marks a literal as long. It is optional here, but it makes the type clear:

long distance = 1500000L;
unsigned long bigCount = 4000000000UL;

The UL suffix makes the literal an unsigned long, and bigCount is declared unsigned, so it cannot hold a negative value.

When long is not enough

Because long is only guaranteed 32 bits, it can run out of room on some systems. The sum of the numbers from 1 to 100000 is 5,000,050,000, which is too large for a 32-bit long on Windows. Using long long keeps the result correct on every platform:

#include <iostream>

int main() {
    long long total = 0;
    for (int i = 1; i <= 100000; ++i) {
        total += i;
    }
    std::cout << "Sum: " << total << "\n";
    return 0;
}

Output:

Sum: 5000050000

For the full story on the 64-bit type, see what is long long in C.

Signed and unsigned long

By default, long is signed, so it can hold negative values. Adding unsigned removes the negatives and roughly doubles the largest positive value. For what that means in practice, see signed vs unsigned in C.

Which type to use

  • Use int for everyday whole numbers.
  • Use long when you want the platform’s usual wider integer and do not need an exact width.
  • Use long long, or std::int64_t from <cstdint>, when you need 64 bits on every platform.

Because long changes width from one platform to another, many programmers choose long long for large values. That way the code behaves the same on each platform.

The complete guide to long long

What Is long long in C++?
Read the guide

Keep exploring

Guides from other topics

All topics →

Unary Operators in C++

Learn what unary operators in C++ are, from the one-operand symbols like !, ~, ++ and * to prefix vs postfix, precedence and common mistakes, with examples.