C Reference ยท Types and Literals
C int, short and long integer types
Reference for the C integer types short, int and long: guaranteed minimum ranges, typical sizes, literal suffixes, printf specifiers and overflow behavior.
C has a family of integer types that differ in how many values they can hold: short, int, long (and long long, see its own page). Each one comes in a signed and an unsigned flavor, and each is at least as big as the one before it. The standard fixes only minimum ranges, so the exact size depends on the platform.
int is the natural integer of the machine and the default choice for counters and small numbers. Choose short only to save memory in big arrays, and long when you need more than 16 bits on every platform.
short s = 1000; int count = 42; long big = 3000000000L;
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
short int a; /* same as: short a; */ int b; long int c; /* same as: long c; */ signed int d; /* same as: int d; */ unsigned short e; unsigned long f;
| Type | Guaranteed minimum range | Typical size | printf |
scanf |
|---|---|---|---|---|
short |
-32767 to 32767 | 2 bytes | %hd |
%hd |
int |
-32767 to 32767 | 4 bytes | %d |
%d |
long |
-2147483647 to 2147483647 | 4 bytes (Windows), 8 bytes (Linux 64-bit) | %ld |
%ld |
The order sizeof(short) <= sizeof(int) <= sizeof(long) is guaranteed, as are the macros in <limits.h>: SHRT_MIN, SHRT_MAX, INT_MIN, INT_MAX, LONG_MIN, LONG_MAX. The sign keyword can be omitted (int means signed int); the word int can be dropped after short, long and unsigned.
Literal suffixes: none for int, L or l for long, LL for long long, U for unsigned. A decimal literal takes the first type in which it fits.
Errors / undefined behavior: signed integer overflow is undefined. Division by zero is undefined. INT_MIN / -1 overflows and is undefined. Converting an out-of-range value to a signed type is implementation-defined.
Usage notes
- Integer promotion. Operands narrower than
intare converted tointbefore arithmetic, which is whyshort + shortdoes not overflow at 16 bits. - Division truncates toward zero since C99, so
-7 / 2is-3and-7 % 2is-1. The remainder has the sign of the dividend. - Do not assume 32 bits.
intis only guaranteed 16 bits. If the size matters, useint32_tand friends. longdiffers between platforms: 64 bits on Linux and macOS (LP64), 32 bits on 64-bit Windows (LLP64). Uselong longorint64_twhen you need 64 bits everywhere.- Match
printfspecifiers to the type.%dwith alongargument is undefined behavior; GCC warns about it with-Wformat. - Leading zero means octal:
010is 8. Binary literals (0b101) are standard in C23 and a common extension before. - C23 digit separators:
1'000'000is a valid literal. - Array indices and sizes should be
size_t, notint, for large data.