Course contentsShow
← C
Lesson 83 of 1,2192. Variables and TypesFree lesson

The `long long` type

The long long type

What you'll learn: How to store massive whole numbers that exceed the capacity of a standard integer.

In C, a standard int is like a medium-sized storage bin. On most modern systems, it can hold values up to about 2.1 billion. While that sounds huge, it isn't enough for everything. If you are calculating the number of milliseconds in a century, the global population, or the number of bytes on a massive hard drive, a regular int will "overflow," causing your program to wrap around to a negative number like a car odometer flipping back to zero.

To handle these giant values, C provides the long long type.

Think Big

Think of long long as a double-wide storage unit. While a standard int typically uses 32 bits of memory, a long long is guaranteed to be at least 64 bits. This extra space doesn't just double the capacity; it increases it exponentially. A long long can hold numbers up to 9 quintillion (that’s a 9 followed by 18 zeros).

Using long long in Code

When you use this type, you need to remember two specific details: the type declaration and the format specifier for printing. To tell the compiler a constant number is a long long, we often add the suffix LL to the end of the number. When printing with printf, we use %lld (think: "long long decimal").

#include <stdio.h>

int main() {
    // A standard int can't hold the world population
    // We use long long to ensure we have enough room
    long long worldPopulation = 8000000000LL;

    // Printing requires the %lld specifier
    printf("The current world population is over %lld humans.\n", worldPopulation);

    // Mathematical operations work exactly the same way
    long long doublePopulation = worldPopulation * 2;
    printf("Double that would be %lld.\n", doublePopulation);

    return 0;
}

When to use it

You might wonder: "Why not use long long for everything?" The reason is efficiency. Large variables take up more RAM and can be slightly slower for the CPU to process. Use a regular int for counting small things (like the items in a shopping cart) and save long long for when you are dealing with astronomical scales or high-precision timestamps.

Key takeaway

Use long long and the %lld specifier when your whole numbers exceed 2 billion.