Escape Sequences
An escape sequence consists of a backslash symbol (\) followed by one of the escape sequence characters (or an octal or hexadecimal number). The commonly used escape sequences and their meaning are listed in the table below.
Symbol | Name | Meaning |
---|---|---|
\a | Alert | Produces an audible or visible alert |
\b | Backspace | Moves the cursor back one position (non-destructive) |
\f | Form feed | Moves the cursor to the first position of the next page |
\n | New line | Moves the cursor to the first position of the next line |
\r | Carriage return | Moves the cursor to the first position of the current line |
\t | Horizontal tab | Moves the cursor to the next horizontal tabular position |
\v | Vertical tab | Moves the cursor to the next vertical tabular position |
\' | Single quote | Produces a single quote |
\" | Double quote | Produces a double quote |
\? | Question mark | Produces a question mark |
\\ | Backslash | Produces a single backslash |
\0 | Null | Produces a null character |
Escape sequences are used with string literals to represent special characters when writing C source code. For example, the following statement prints the statement "Here is some text." on two lines, and then inserts three blank lines.
printf("Here is\nsome text.\n\n\n");
Note that the backslash is also used as a continuation marker to break long string literals into shorter sections to aid readability. The continuation marker is not an escape sequence, it simply tells the compiler that the string continues on the next line in the source code, as demonstrated by the short program below.
#include <stdio.h>
#define MESSAGE "A long message that won't fit \
on one line in the C program editor."
void main()
{
char str[1];
printf( "%s", MESSAGE);
printf( "\n\nPress ENTER to continue.");
gets(str);
}