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.


The C++ Escape Sequences
SymbolNameMeaning
\aAlertProduces an audible or visible alert
\bBackspaceMoves the cursor back one position (non-destructive)
\fForm feedMoves the cursor to the first position of the next page
\nNew lineMoves the cursor to the first position of the next line
\rCarriage returnMoves the cursor to the first position of the current line
\tHorizontal tabMoves the cursor to the next horizontal tabular position
\vVertical tabMoves the cursor to the next vertical tabular position
\'Single quoteProduces a single quote
\"Double quoteProduces a double quote
\?Question markProduces a question mark
\\BackslashProduces a single backslash
\0NullProduces 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);
}