C/C++ Arena

Step 4 of 5

Escape sequences

Inside a string, some characters are special. A " would end the string early, and there's no key for "new line" that you can put between quotes. C solves this with escape sequences: a backslash \ followed by a character, meaning "the next character is not what it looks like".

You write You get
\n new line
\t tab (jump to the next column)
\" a double quote character
\\ a single backslash
#include <stdio.h>

int main(void) {
    printf("Name\tScore\n");
    printf("Ada\t95\n");
    printf("A path: C:\\games\\cs\n");
    printf("He said \"nice shot\"\n");
    return 0;
}
Name	Score
Ada	95
A path: C:\games\cs
He said "nice shot"

Each escape sequence is two characters in your source code but becomes one character in the output. The compiler does the translation.

Common mistakes

Your turn: print this line exactly, including the quotes:

She said "gg" and left

Previous: Several lines Next: Comments and reading errors