C Special Characters (Escape Sequences)
Some characters cannot be typed directly inside a string literal. A double quote would end the string early, a newline would break the line in your source code, and a tab is invisible. C solves this with escape sequences: a backslash \ followed by one or more characters that together stand for a single special character.
The problem
This does not compile, because the second " ends the string:
printf("She said "hello" to me\n"); // errorThe fix is to escape the inner quotes with a backslash:
#include <stdio.h>
int main(void) {
printf("She said \"hello\" to me\n");
printf("It's fine to use ' inside double quotes\n");
printf("A single-quote char: %c\n", '\'');
return 0;
}She said "hello" to me
It's fine to use ' inside double quotes
A single-quote char: '
Common escape sequences
| Sequence | Meaning |
|---|---|
\n |
newline (line feed) |
\t |
horizontal tab |
\\ |
a backslash |
\" |
a double quote |
\' |
a single quote |
\0 |
the null character (end of a string) |
\r |
carriage return (go to start of line) |
\a |
alert (bell) |
\b |
backspace |
\xhh |
character with hexadecimal code hh (e.g. \x41 is A) |
\ooo |
character with octal code ooo (e.g. \101 is A) |
Tabs for simple alignment
\t moves the cursor to the next tab stop (usually every 8 columns). It is a quick way to line up columns:
#include <stdio.h>
int main(void) {
printf("Name\tAge\tCity\n");
printf("Lea\t31\tLyon\n");
printf("Tom\t7\tTurin\n");
return 0;
}Name Age City
Lea 31 Lyon
Tom 7 Turin
For exact alignment, field widths such as %-10s (see C Format Specifiers) are more reliable than tabs, because tab stops depend on the terminal.
Backslashes in paths
Since \ starts an escape sequence, a literal backslash has to be written twice. This matters for Windows paths:
#include <stdio.h>
int main(void) {
printf("C:\\Users\\Ada\\notes.txt\n");
printf("A smiley: \\o/\n");
return 0;
}C:\Users\Ada\notes.txt
A smiley: \o/
Tip: Windows also accepts forward slashes in most file paths, so
"C:/Users/Ada/notes.txt"avoids the doubled backslashes.
Character codes in hex and octal
\x followed by hex digits, or \ followed by up to three octal digits, inserts a character by its numeric code. That is handy for bytes that have no printable form:
#include <stdio.h>
int main(void) {
printf("\x48\x69\x21\n"); // H i !
printf("\103\117\104\105\n"); // C O D E in octal
char bytes[] = "A\x00Z";
printf("printed: %s\n", bytes); // stops at the embedded \0
printf("sizeof: %zu\n", sizeof(bytes));
return 0;
}Hi!
CODE
printed: A
sizeof: 4
The last example shows why \0 is special: %s stops at the first null byte, even though the array still contains 'Z' after it.
Carriage return
\r moves the cursor back to the start of the current line without starting a new one. Terminal programs use this for progress bars that redraw themselves. In captured output (like these playgrounds) the effect is not visible, so we will not run it here:
for (int p = 0; p <= 100; p += 25) {
printf("\rProgress: %3d%%", p); // overwrites the same line in a terminal
}
printf("\n");Escape sequences in character literals
Escape sequences work in single-quoted char literals too. Each one is a single character, even though you type two or more symbols:
#include <stdio.h>
int main(void) {
char newline = '\n';
char tab = '\t';
char nul = '\0';
printf("codes: newline=%d tab=%d nul=%d backslash=%d\n", newline, tab, nul, '\\');
printf("length of \"a\\tb\" is %zu bytes incl. terminator\n", sizeof("a\tb"));
return 0;
}codes: newline=10 tab=9 nul=0 backslash=92
length of "a\tb" is 4 bytes incl. terminator
Test yourself
How do you write a string containing a double quote character?
- """
- "\""
- '"'
- "\q"
What does printf("a\\b"); print?
- a\\b
- a\b
- ab
- a then a backspace
How many characters does the escape sequence \n represent?
- 2
- 1
- 0
- It depends on the platform