C Reference ยท Types and Literals
Literals in C
Reference for C literals: integer suffixes and bases, 0b binary and digit separators, floating-point forms, character and string escapes, and literal types.
A literal is a value written directly in the source code. C has integer, floating-point, character and string literals, plus the keywords true, false and nullptr in C23. The form you write decides the type: 42 is an int, 42L a long, 4.2 a double, 4.2f a float, and 'A' an int (not a char, unlike C++).
Knowing the type of a literal matters for overflow, for sizeof, and for the usual arithmetic conversions. 3 / 2 is 1 because both operands are int, while 3 / 2.0 is 1.5.
int hex = 0xFF; /* 255 */ long long big = 9000000000LL; double x = 1.5e3; /* 1500.0 */
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
| Kind | Examples | Type |
|---|---|---|
| Decimal integer | 42, 42u, 42L, 42UL, 42LL, 42ULL |
smallest of int, long, long long that fits (suffix chooses a minimum) |
| Octal / hex / binary | 052, 0x2A, 0b101010 |
first of int, unsigned int, long, ... that fits |
_BitInt |
42wb, 42uwb |
_BitInt(N) with just enough bits (C23) |
| Floating | 1.5, 1.5f, 1.5L, 1e-3, 0x1.8p1 |
double, float, long double |
| Character | 'a', '\n', '\x41', '\101', u8'a' |
int (char8_t for u8'a' in C23) |
| String | "text", u8"text", L"text" |
char[N], char8_t[N], wchar_t[N] |
| Keyword constants | true, false, nullptr |
bool, bool, nullptr_t (C23) |
Common escapes: \n newline, \t tab, \\ backslash, \' and \" quotes, \0 null, \xHH hex byte, \ooo octal byte, \uHHHH and \UHHHHHHHH universal character names.
Errors / undefined behavior: an integer constant too large for every permitted type is a constraint violation. Modifying a string literal is undefined behavior. A decimal literal such as 2147483648 (no suffix) is a long, not an int.
Usage notes
- Decimal integers without a suffix never become unsigned, but hex and octal ones can.
0xFFFFFFFFis anunsigned inton a 32-bitint. - Prefer
1ULL << 40to1 << 40: the literal's type decides the width of the shift. - A leading zero means octal.
010is 8, and09is a compile error. Never pad decimal numbers with zeros. - Use a suffix on floats when it matters:
0.1fis afloat,0.1adouble, and they are different numbers. 'A'has typeintin C, sosizeof 'A'issizeof(int).- Multi-character constants such as
'ab'are implementation-defined; avoid them. 0bbinary literals and'separators are standard in C23. The%bformat andstrtolwith0bare library features and are not supported by the older C library in the runner.- Adjacent string literals are joined at translation time, which is the standard way to split long strings or build them with macros.