C escape sequences: controls, quotes, and numeric escapes
C escape sequences: controls, quotes, and numeric escapes
C escapes denote characters in character constants and string literals. Octal escapes use up to three digits; hex escapes consume all following hex digits.
| Category | Cheat sheets |
|---|---|
| Kind | Cheat sheets |
| C standard | C89 / C90 |
| Sequence | Name | Meaning | Example |
|---|---|---|---|
| \a | Alert | Produces an audible or visual alert; an audible bell is not guaranteed. | '\a' |
| \b | Backspace | Moves the active position one place back on the current line; it does not inherently erase a character. | '\b' |
| \f | Form feed | Moves the active position to the start of the next logical page, mainly for paginated output devices. | '\f' |
| \n | Newline | Moves to the start of the next line; the text stream handles the environment's line-ending representation. | '\n' |
| \r | Carriage return | Moves the active position to the start of the current line without advancing to the next line. | '\r' |
| \t | Horizontal tab | Moves to the next horizontal tab position on the current line; its visual width depends on the environment. | '\t' |
| \v | Vertical tab | Moves to the next vertical tab position; the visible effect depends on the output device. | '\v' |
| \\ | Backslash | Represents a literal backslash instead of the start of an escape. | '\\' |
| \' | Single quote | Represents a single quote in a character constant; it is valid but usually unnecessary inside a string. | '\'' |
| \" | Double quote | Represents a double quote inside a string literal without terminating the string. | '\"' |
| \? | Question mark | Represents a literal question mark, especially useful when avoiding C89 trigraph interpretation. | '\?' |
| \0 | Null character | An octal escape with value 0, commonly used as a string terminator; it is not the character '0'. | '\0' |
| \ooo | Octal numeric escape | Uses one to three octal digits to specify a character code; for example, A has numeric value 65 in ASCII. | '\101' |
| \xhh... | Hexadecimal numeric escape | Uses one or more hexadecimal digits to specify a character code; for example, A has numeric value 65 in ASCII, and there is no three-digit limit. | '\x41' |
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
Line one
Line two
Quote: "C"
Backslash: \
Octal value: 65
Hex value: 65
Null value: 0
⚠️ Warning A hexadecimal escape consumes all following hexadecimal digits: in "\x41B", B is part of the escape value. Use adjacent string literals, "\x41" "B", to terminate it explicitly.
⚠️ Warning A numeric escape must not exceed the range representable by the target character type. In an ordinary character constant or string, the value must fit unsigned char. The visible character for a numeric code depends on the execution character set, so do not assume ASCII everywhere.
🏷️ Related cheat sheets
📚 Related Tutorials
❓ FAQ
QWhy does "\x41B" not necessarily produce AB, and how should it be split?
AB is a hexadecimal digit, so it becomes part of the single escape \x41B. Writing "\x41" "B" creates adjacent string literals that are concatenated, with the first escape ending at the quote.
QAre '\0' and '0' the same character in C?
ANo. '\0' has value 0 and is the null character used to terminate strings. '0' is the visible digit zero, whose code is defined by the execution character set and is 48 in ASCII.
QHow many digits can octal and hexadecimal escapes contain?
AAn octal escape can contain at most three octal digits. A hexadecimal escape has no three-digit limit and consumes every subsequent hexadecimal digit. A limited digit count does not guarantee a valid value: the escape must still fit the target character type. Use adjacent string literals to end a hexadecimal escape explicitly.