Skip to content
Tutorials Center
My learning

Topics

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.

C
int hex = 0xFF;              /* 255 */
long long big = 9000000000LL;
double x = 1.5e3;            /* 1500.0 */

Try it

C Integer and floating-point literals

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.

C Characters, strings and escapes

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. 0xFFFFFFFF is an unsigned int on a 32-bit int.
  • Prefer 1ULL << 40 to 1 << 40: the literal's type decides the width of the shift.
  • A leading zero means octal. 010 is 8, and 09 is a compile error. Never pad decimal numbers with zeros.
  • Use a suffix on floats when it matters: 0.1f is a float, 0.1 a double, and they are different numbers.
  • 'A' has type int in C, so sizeof 'A' is sizeof(int).
  • Multi-character constants such as 'ab' are implementation-defined; avoid them.
  • 0b binary literals and ' separators are standard in C23. The %b format and strtol with 0b are 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.