Lesson 66 +10 XP

Special Characters in Strings

Some characters are hard to type inside a string. That's where escape sequences come in.

The problem

How do you put a double quote inside a double-quoted string?


c
char txt[] = "We are the "people" of the world.";

The \" escape prints a literal quote.

Common escapes

  • \\ - a single backslash
  • \" - a double quote
  • `

` - newline

  • \t - tab
  • \0 - null

Example


c
printf("Line1

### Line2\tTabbed
");

Why bother?

Without escapes, the compiler would get confused by bare quotes or backslashes inside strings. The backslash tells C "the next char is special".

TL;DR

  • Use \" to print a quote inside a string.
  • \\ prints a backslash.
  • `

and \t` are the common whitespace escapes.

  • The backslash marks that the next character is special.