Lesson 66 +10 XP

Special Characters in Strings

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?

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

The \" escape prints a literal quote.

Common escapes

  • \\ - a single backslash
  • \" - a double quote
  • \n - newline
  • \t - tab
  • \0 - null

Example

printf("Line1\nLine2\tTabbed\n");

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.
  • \n and \t are the common whitespace escapes.
  • The backslash marks that the next character is special.