Lesson 19 +10 XP

New Lines and Special Characters

New Lines and Special Characters

You've got text, now let's decide where lines break and add special characters with escape sequences.

The \n newline

\n inside a string starts a new line:

printf("Hello\nWorld");

Output:

Hello
World

Other useful escapes

  • \t - a tab (horizontal space)
  • \\ - a single backslash
  • \" - a double quote inside a string
  • \0 - the null character (marks the end of a string)

Example

printf("Name:\tBilbo\nQuote: \"Not all those who wander are lost\"\n");

Multiple newlines

Each \n jumps one line, so two of them create a blank line:

printf("a\n\nb");

Output: an empty line sits between a and b.

TL;DR

  • \n starts a new line.
  • \t adds a tab.
  • \\ and \" let you print backslashes and quotes.
  • \0 is the null character that ends strings.