Escape sequences, literals & ASCII
Facts per ISO C17 · C23 changes marked · UB = undefined behavior, ID = implementation-defined
Styled for paper — hit Ctrl+P and pin it above your desk.
An escape sequence is two or more source characters that become one character in the compiled program: the \n you've typed since hello-world is backslash + n in your file, but a single byte (10) at runtime.
Simple escapes
| escape | byte | name | notes | learn |
\n | 10 | newline (LF) | ends a line; also flushes line-buffered stdout on terminals | → lesson |
\t | 9 | horizontal tab | quick-and-dirty columns | → lesson |
\r | 13 | carriage return | return to column 0 — progress-bar trick \rdone: 42%; half of Windows' \r\n line ending | |
\\ | 92 | backslash | a literal \ — needed for Windows paths in literals | |
\' | 39 | single quote | needed in char literals: '\'' | |
\" | 34 | double quote | needed in string literals: "she said \"hi\"" | |
\? | 63 | question mark | exists only to defuse trigraphs (see below) — pointless since C23 | |
\a | 7 | alert (bell) | historically rang the terminal bell | |
\b | 8 | backspace | moves the cursor back one column | |
\f | 12 | form feed | page break on line printers | |
\v | 11 | vertical tab | museum piece | |
\0 | 0 | NUL | really the shortest octal escape — the string terminator | → lesson |
\e | 27 | escape (ESC) | not ISO C — a GNU extension. Portable: \x1b or \033 (start of ANSI color codes) | |
Anything else after a backslash (\q, \m…) is not a valid escape sequence — the standard gives it no meaning (compilers typically warn and guess).
Numeric escapes
| form | example | rule | trap |
octal \o \oo \ooo | '\033' = 27 (ESC) | one to three octal digits; stops at the third digit or the first non-octal character | "\0777" is two characters: \077 then '7'. And the value must fit in a byte — '\777' (511) is an error |
hex \xh… | '\x1b' = 27 | one or more hex digits — no length limit | greedy! "\x0aFF" tries to make one huge escape and fails to compile. Split the literal: "\x0a" "FF" |
universal \uXXXX | "\u00e9" = é | exactly 4 hex digits naming a Unicode code point (C99) | surrogates (D800–DFFF) and most points below A0 are forbidden |
universal \UXXXXXXXX | "\U0001F600" = 😀 | exactly 8 hex digits (C99) | how it's encoded in a plain "…" depends on the execution charset — use u8"…" for guaranteed UTF-8 |
| delimited C23 | "\x{1b}" "\o{33}" "\u{1F600}" | braces end the greediness problem | new in C23 — older compilers reject |
Character literals
| literal | type | notes | learn |
'a' | int — not char! | a C surprise (C++ differs): sizeof 'a' == sizeof(int). A char really is a small integer — 'A' is just 65 | → lesson |
'ab' | int | multi-character constant: legal, value is ID — a portability trap, avoid | |
L'x' | wchar_t | wide character — 32-bit on Linux, 16-bit on Windows (ID) | |
u'x' | char16_t | C11, from <uchar.h> | |
U'x' | char32_t | C11, from <uchar.h> | |
u8'x' C23 | char8_t (= unsigned char) | C23 only; must be a single ASCII-range character | → lesson |
Whether plain char is signed or unsigned is ID — signed on x86 Linux, unsigned on ARM Linux. Code that stores getchar()'s result in a char before comparing to EOF breaks either way: use int.
String literals
| literal | array type | notes | learn |
"hi" | char[3] | the terminator is included: sizeof "hi" is 3, strlen("hi") is 2 | → lesson |
L"hi" | wchar_t[3] | wide string — print with %ls | |
u8"hi" | char[3] (C11) · char8_t[3] C23 | guaranteed UTF-8 encoding regardless of the compiler's charset | → lesson |
u"hi" / U"hi" | char16_t[3] / char32_t[3] | C11; UTF-16/UTF-32 in practice (guaranteed in C23) | |
| rule | details | learn |
| modifying a literal is UB | char *s = "hi"; s[0] = 'H'; typically crashes (literals live in read-only memory). The array isn't const-typed in C — the compiler won't stop you. Prefer const char *s, or copy into char s[] = "hi"; | → lesson |
| identical literals may be merged | whether two "abc"s share one address is ID — never compare strings with == | → lesson |
| adjacent literals concatenate | "abc" "def" is one literal — how the PRId64 macros work, and how you split long lines. One prefixed + one plain: the prefix wins | |
| backslash-newline continues a line | a \ as the very last character splices the next source line on — works anywhere, not just strings (macros love it) | |
🪦
Trigraphs — 1989–2023, rest in peace. For keyboards lacking #[]{}|~^\, C89 made these nine sequences convert everywhere, even inside string literals: ??=→# ??(→[ ??)→] ??<→{ ??>→} ??/→\ ??'→^ ??!→| ??-→~. So "huh??!" silently became "huh|" — hence the \? escape. C23 removed trigraphs entirely; GCC only ever applies them in strict -std= modes (or with -trigraphs). Their cousins the digraphs (<% %> <: :> %:) are whole tokens, never touch strings, and are still legal.
ASCII 0–127
Every C string is bytes; these are the byte values (text-encoding lesson). Grey rows are control characters; the escape column shows the C spelling where one exists.
| dec | hex | char | esc |
| 0 | 0x00 | NUL | \0 |
| 1 | 0x01 | SOH | |
| 2 | 0x02 | STX | |
| 3 | 0x03 | ETX | |
| 4 | 0x04 | EOT | |
| 5 | 0x05 | ENQ | |
| 6 | 0x06 | ACK | |
| 7 | 0x07 | BEL | \a |
| 8 | 0x08 | BS | \b |
| 9 | 0x09 | HT | \t |
| 10 | 0x0A | LF | \n |
| 11 | 0x0B | VT | \v |
| 12 | 0x0C | FF | \f |
| 13 | 0x0D | CR | \r |
| 14 | 0x0E | SO | |
| 15 | 0x0F | SI | |
| 16 | 0x10 | DLE | |
| 17 | 0x11 | DC1 | |
| 18 | 0x12 | DC2 | |
| 19 | 0x13 | DC3 | |
| 20 | 0x14 | DC4 | |
| 21 | 0x15 | NAK | |
| 22 | 0x16 | SYN | |
| 23 | 0x17 | ETB | |
| 24 | 0x18 | CAN | |
| 25 | 0x19 | EM | |
| 26 | 0x1A | SUB | |
| 27 | 0x1B | ESC | \x1b |
| 28 | 0x1C | FS | |
| 29 | 0x1D | GS | |
| 30 | 0x1E | RS | |
| 31 | 0x1F | US | |
| dec | hex | char | esc |
| 32 | 0x20 | SP | |
| 33 | 0x21 | ! | |
| 34 | 0x22 | " | \" |
| 35 | 0x23 | # | |
| 36 | 0x24 | $ | |
| 37 | 0x25 | % | |
| 38 | 0x26 | & | |
| 39 | 0x27 | ' | \' |
| 40 | 0x28 | ( | |
| 41 | 0x29 | ) | |
| 42 | 0x2A | * | |
| 43 | 0x2B | + | |
| 44 | 0x2C | , | |
| 45 | 0x2D | - | |
| 46 | 0x2E | . | |
| 47 | 0x2F | / | |
| 48 | 0x30 | 0 | |
| 49 | 0x31 | 1 | |
| 50 | 0x32 | 2 | |
| 51 | 0x33 | 3 | |
| 52 | 0x34 | 4 | |
| 53 | 0x35 | 5 | |
| 54 | 0x36 | 6 | |
| 55 | 0x37 | 7 | |
| 56 | 0x38 | 8 | |
| 57 | 0x39 | 9 | |
| 58 | 0x3A | : | |
| 59 | 0x3B | ; | |
| 60 | 0x3C | < | |
| 61 | 0x3D | = | |
| 62 | 0x3E | > | |
| 63 | 0x3F | ? | \? |
| dec | hex | char | esc |
| 64 | 0x40 | @ | |
| 65 | 0x41 | A | |
| 66 | 0x42 | B | |
| 67 | 0x43 | C | |
| 68 | 0x44 | D | |
| 69 | 0x45 | E | |
| 70 | 0x46 | F | |
| 71 | 0x47 | G | |
| 72 | 0x48 | H | |
| 73 | 0x49 | I | |
| 74 | 0x4A | J | |
| 75 | 0x4B | K | |
| 76 | 0x4C | L | |
| 77 | 0x4D | M | |
| 78 | 0x4E | N | |
| 79 | 0x4F | O | |
| 80 | 0x50 | P | |
| 81 | 0x51 | Q | |
| 82 | 0x52 | R | |
| 83 | 0x53 | S | |
| 84 | 0x54 | T | |
| 85 | 0x55 | U | |
| 86 | 0x56 | V | |
| 87 | 0x57 | W | |
| 88 | 0x58 | X | |
| 89 | 0x59 | Y | |
| 90 | 0x5A | Z | |
| 91 | 0x5B | [ | |
| 92 | 0x5C | \ | \\ |
| 93 | 0x5D | ] | |
| 94 | 0x5E | ^ | |
| 95 | 0x5F | _ | |
| dec | hex | char | esc |
| 96 | 0x60 | ` | |
| 97 | 0x61 | a | |
| 98 | 0x62 | b | |
| 99 | 0x63 | c | |
| 100 | 0x64 | d | |
| 101 | 0x65 | e | |
| 102 | 0x66 | f | |
| 103 | 0x67 | g | |
| 104 | 0x68 | h | |
| 105 | 0x69 | i | |
| 106 | 0x6A | j | |
| 107 | 0x6B | k | |
| 108 | 0x6C | l | |
| 109 | 0x6D | m | |
| 110 | 0x6E | n | |
| 111 | 0x6F | o | |
| 112 | 0x70 | p | |
| 113 | 0x71 | q | |
| 114 | 0x72 | r | |
| 115 | 0x73 | s | |
| 116 | 0x74 | t | |
| 117 | 0x75 | u | |
| 118 | 0x76 | v | |
| 119 | 0x77 | w | |
| 120 | 0x78 | x | |
| 121 | 0x79 | y | |
| 122 | 0x7A | z | |
| 123 | 0x7B | { | |
| 124 | 0x7C | | | |
| 125 | 0x7D | } | |
| 126 | 0x7E | ~ | |
| 127 | 0x7F | DEL | |