C Reference ยท Types and Literals
C long long: 64-bit integers
Reference for the C long long and unsigned long long types: guaranteed 64-bit range, LL and ULL literal suffixes, %lld and %llu, and overflow behavior.
long long is an integer type that is guaranteed to hold at least 64 bits. It was added in C99 so that portable programs could count past four billion without depending on what long happens to be. On every mainstream platform, long long is exactly 64 bits: values from about -9.2 ร 10^18 to 9.2 ร 10^18 when signed, and up to about 1.8 ร 10^19 when unsigned.
Use it for file sizes, timestamps in milliseconds, factorials, large sums and any computation where int overflows. For exact-width needs, prefer int64_t.
long long total = 9000000000LL; unsigned long long mask = 0xFFFFFFFFFFFFFFFFULL;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
long long a; long long int b; /* same type */ signed long long c; unsigned long long d;
| Item | Description |
|---|---|
| Minimum range | -9223372036854775807 to 9223372036854775807 (signed); 0 to 18446744073709551615 (unsigned) |
| Limits | LLONG_MIN, LLONG_MAX, ULLONG_MAX in <limits.h>; C23 adds LLONG_WIDTH and ULLONG_WIDTH |
| Literal suffix | LL or ll (signed), ULL or ull (unsigned); the two L letters must have the same case (lL is invalid) |
printf |
%lld, %llu, %llx |
scanf |
%lld, %llu |
Errors / undefined behavior: signed overflow is undefined, so LLONG_MAX + 1 is not safe. Unsigned arithmetic wraps modulo 2^64. Using %ld with a long long argument is undefined behavior on platforms where the sizes differ.
Usage notes
- Literal types matter.
3000000000has typelong(orlong long) automatically because it does not fit inint; but1 << 40withintoperands overflows, so write1LL << 40. - Widen before you multiply.
(long long)a * bis a 64-bit product;(long long)(a * b)multiplies inintfirst and may overflow. longvslong long:longis 32 bits on 64-bit Windows and 64 bits on Linux.long longis 64 bits everywhere.- Speed: on 64-bit CPUs there is no penalty; on 32-bit targets arithmetic is a few instructions slower.
- Mixed arithmetic: an
intcombined with along longis converted tolong long; a signedlong longcombined withunsigned long longbecomes unsigned, which may surprise you with negative values. - Format macros: for
int64_t, usePRId64from<inttypes.h>rather than assuming%lld. - Division and shifts: shifting by 64 or more is undefined.
LLONG_MIN / -1overflows. - Added in C99, and supported by every compiler in current use, including MSVC.