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
- Writing
"She said "hi"": the second"ends the string, and the compiler gets confused by what follows. Escape the inner quotes. - Using a forward slash
/ninstead of a backslash\n. That just prints/n. - A single
\before a normal letter (like\q) is not a valid escape, and the compiler warns about it.
Your turn: print this line exactly, including the quotes:
She said "gg" and left