liveBook · Manning

Escape sequence

As explained in Modern C

An escape sequence is a C source notation beginning with a backslash that encodes a character or special value.

Spacing a line of output

Consider `printf("element %zu is %g, \tits square is %g\n", i, A[i], A[i]*A[i]);`. Its output places space before “its square” and prints each result on a new line. The `\t` and `\n` in the string literal are escape sequences; the `%zu` and `%g` markers are format specifiers, not escapes. section 1.1

TAKEAWAY: An escape sequence begins with a backslash in C source and denotes a character or special value. In a `printf` format string, `\t` supplies a tab and `\n` supplies a newline. A percent sign instead introduces a format specifier, which tells `printf` where to insert an argument. section 1.1

Escapes can also specify Unicode characters. In a multibyte string, `\u` followed by four hexadecimal digits names a Unicode code point: `"\u00E4"` specifies “latin small letter a with diaeresis.” For code points requiring more digits, `\U` takes eight hexadecimal digits. These Unicode escape forms were introduced in C11. section 14.3

43   #define VBAR "\u2502"      /**< a vertical bar character   */
44   #define HBAR "\u2500"      /**< a horizontal bar character */
45   #define TOPLEFT "\u250c"   /**< topleft corner character   */
46   #define TOPRIGHT "\u2510"  /**< topright corner character  */


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":5},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":5},{\"line\":2,\"ch\":12}],[{\"line\":3,\"ch\":5},{\"line\":3,\"ch\":12}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":5},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":5},{\"line\":2,\"ch\":12}],[{\"line\":3,\"ch\":5},{\"line\":3,\"ch\":12}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":5},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":5},{\"line\":2,\"ch\":12}],[{\"line\":3,\"ch\":5},{\"line\":3,\"ch\":12}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":5},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":5},{\"line\":2,\"ch\":12}],[{\"line\":3,\"ch\":5},{\"line\":3,\"ch\":12}]]"}
!@%STYLE%@!

These string-literal macros use four-digit Unicode escapes for vertical and horizontal bars and two corners—characters that may be inconvenient to type directly. section 14.3

A Unicode escape lets you write a code point in the source; it does not make multibyte string processing trivial. For example, `strlen` does not give the number of characters in a multibyte string. section 14.3

How Modern C covers it

How deep it goes
Explains two output escapes and the four- and eight-digit Unicode forms, but does not develop a general escape catalogue.
Examples
Shows `\t` and `\n` in formatted output, `"\u00E4"`, and macros for graphical characters.
What it assumes
Introduces string literals and format specifiers alongside the first escapes, then assumes familiarity with multibyte strings for Unicode examples.
What you can do after
Apply it to write tabs, newlines, and specified Unicode characters in C literals.
How it connects
Connects escape sequences to string literals, `printf` format specifiers, Unicode code points, and multibyte strings.

Also explained in

Compare how 6 books explain escape sequence →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.