Escape sequences represent special characters or character values that cannot be written directly in the usual character-literal form. They begin with a backslash \.
#include <stdio.h>
int main()
{
printf("First line\nSecond line\n");
printf("Column 1\tColumn 2\n");
printf("He said, \"Hello!\"\n");
printf("C:\\Program Files\\C\n");
return 0;
}
Example Output #
First line
Second line
Column 1 Column 2
He said, "Hello!"
C:\Program Files\C
Explanation #
The escape sequences in the example are interpreted specially by the C compiler:
\n
moves the output to the next line.
\t
inserts a horizontal tab.
\"
represents a double-quote character inside a string literal.
\\
represents a backslash character.
Common Escape Sequences #
| Escape sequence | Meaning |
|---|---|
\a |
Alert |
\b |
Backspace |
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Horizontal tab |
\v |
Vertical tab |
\\ |
Backslash |
\' |
Single quote |
\" |
Double quote |
\? |
Question mark |
\0 |
Null character |
Escape sequences can also represent character values using octal and hexadecimal notation.
Octal and Hexadecimal Escape Sequences #
| Form | Meaning | Example |
|---|---|---|
\ooo |
Octal character value | '\101' |
\xhh... |
Hexadecimal character value | '\x41' |
For example:
char a = '\101';
char b = '\x41';
Both represent the character A on an ASCII execution character set.
The hexadecimal escape sequence continues consuming hexadecimal digits until a non-hexadecimal character is encountered, so its boundary can matter when writing adjacent characters.