• C Programming Video Tutorials

C - Escape Sequences



An escape sequence in C is a literal made up of more than one character put inside single quotes. Normally, a character literal consists of only a single character inside single quote symbols. However, the escape sequence attaches a special meaning to the character that appears after a backslash character (\). The \ symbol causes the compiler to escape out of the string and provide meaning attached to the character following it. Look at \n as an example. When put inside a string, the \n acts as a newline character, generating the effect of the Enter key press. The statement −

printf("Hello\nWorld");

produces

Hello
World

The new line is an un-printable character. The \n escape sequence is useful to generate its effect. Similarly, the escape sequence \t is equivalent to pressing the Tab key on the keyboard.

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

All Escape Sequences

In C, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters have an interpretation of the escape sequence as per the following table. Here is a list of escape sequences available in C −

Escape sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

Let us understand the escape sequences with the following examples −

Newline Escape Sequence (\n)

The newline character, represented by the escape sequence \n in C, is used to insert the effect of carriage return on the output screen, so as to print text in separate lines and improve the readability of output.

Example

#include <stdio.h>
int main()
{
   printf("Hello.\nGood morning.\nMy name is Ravi");
}

Output

Hello.
Good morning.
My name is Ravi

Tab Escape Sequence (\t)

The tab character (\t) represents the Tab key on the keyboard. When the tab character is encountered in a string, it causes the cursor to move to the next horizontal tab stop. Horizontal tab stops are usually set at intervals of eight characters.

Example

#include <stdio.h>
int main()
{
   printf("Name:\tRavi\tMarks:\t50");
}

Output

Name:   Ravi    Marks:  50

Backslash Escape Sequence (\\)

To add backslash character itself as a part of a string, it must precede by another backslash. First \n escapes out of the string, and the second one takes the effect.

Example

#include <stdio.h>
int main()
{
   printf("Directory in Windows: C:\\users\\user");
}

Output

Directory in Windows: C:\users\user

Double and single Quotes Escape Sequences (\" and \')

These characters have a special meaning in C since " and ' symbols are used for the representation of a character literal and a string literal respectively. Hence, to treat these characters as a part of string, they must be escaped with an additional backslash preceding them.

Example

#include <stdio.h>
int main()
{
   printf("Welcome to \"TutorialsPoint\"\n");
   printf ("\'Welcome\' to TutorialsPoint");
}

Output

Welcome to "TutorialsPoint"
'Welcome' to TutorialsPoint

Backspace Escape Sequence (\b)

The escape sequence "\b", represents backspace character, and is used erase a character or a specific portion of text that has already been printed on the screen.

Example

#include <stdio.h>
int main()
{
   printf("Welcome to\b TutorialsPoint");
}

Output

Welcome t TutorialsPoint

Note that o from to has been erased.

C also has a \r escape sequence. The newline escape sequence (\n) moves the cursor to the beginning of the next line, while the carriage return escape sequence (\r) moves the cursor to the beginning of the current line.

Octal Number Escape Sequence (\ooo)

This escape sequence is used for Octal numbers of one to three digits. An octal escape sequence is a backslash followed by one, two, or three octal digits (0-7). It matches a character in the target sequence with the value specified by those digits.

Example

#include <stdio.h>
int main()
{
   printf("%c", '\141');
   return 0;
}

Output

a

Hexadecimal Number Escape Sequence (\xhh)

A hexadecimal escape sequence is a backslash followed by the letter 'x' followed by two hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence with the value specified by the two digits.

Example

printf("%c", '\x41');

Output

A

Alert or Bell Number Escape Sequence (\a)

The \a escape sequence, which represents the alert or bell character. When executed, it produces a sound or visual alert depending on the terminal or console being used.

Example

printf("Hello \a world\n");

Escape sequences as in C has been implemented in many other languages such as Java, PHP, C#, etc.

Advertisements