Escape Sequence in C

An escape sequence in C is a sequence of characters that does not signify itself when used inside a character or string literal but is translated into another character or a sequence of characters that may be challenging or difficult to represent directly.

In C, each escape sequence contains a backslash of two or more characters,\ (called the’ Escape Character’), while the rest of the characters decide the manner in which the escape sequence is interpreted. For instance, \n is a newline character escape sequence.

Table of Escape Sequence in C

Escape Sequence Meaning
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\’ Single Quote
\” Double Quote
\? Question Mark
\nnn octal number
\xhh hexadecimal number
\0 Null

Example

#include <stdio.h>

int main() {

    printf("Hello,\nworld!");

}

The escape sequence \n in this code does not represent a backslash and the letter n followed because the backslash creates a “break” from the ordinary meaning of compiler characters. After the backslash is displayed, the compiler requires a second character to complete the escape sequence and converts it into the bytes it wants to represent. So “Hello,\nworld!” stands for a string with a built-in newline, whether used inside or elsewhere.

At least one hex digit following \x is needed for a hex escaping sequence without an upper bound. It continues as many hex digits as are. For instance, \xABCDEFG denotes the byte with the ABCDEF 16 numerical value, and the letter G, not a hex digit, is followed. Nevertheless, the actual numerical value allocated is determined by the implementation, when the resulting integer value is too large to fit into one byte. Many platforms have 8-bit char sort, restricted by two hex digits to the useful hex escape sequence. However, within a wide or small string literal (prefixed with L) hex escape sequences longer than two hex digits can be useful: