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 quotes. 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 pressing the Enter key. The following statement −
printf(" Hello \n World ");
Will produce this output −
Hello
World
The new line is an unprintable 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
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 how these escape sequences work with the help of a set of 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. You would use this escape sequence to print text in separate lines and improve the readability of output.
Example
Take a look at the following example −
#include <stdio.h>intmain(){printf("Hello.\nGood morning.\nMy name is Ravi");}
Output
On running this code, you will get the following 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
Take a look at the following example −
#include <stdio.h>intmain(){printf("Name:\tRavi\tMarks:\t50");}
Output
Run the code and check its 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 backslash escapes out of the string, and the second one takes the effect.
Example
Take a look at the following example −
#include <stdio.h>intmain(){printf("Directory in Windows: C:\\users\\user");}
Output
On running this code, you will get the following 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 the string, they must be escaped with an additional backslash preceding them.
Example
Take a look at the following example −
#include <stdio.h>intmain(){printf("Welcome to \"TutorialsPoint\"\n");printf("\'Welcome\' to TutorialsPoint");}
Output
Run the code and check its output −
Welcome to "TutorialsPoint"
'Welcome' to TutorialsPoint
Backspace Escape Sequence (\b)
The escape sequence “\b”, represents the backspace character. It is used erase a character or a specific portion of a text that has already been printed on the screen.
Example
Check the following example code −
#include <stdio.h>intmain(){printf("Welcome to\b TutorialsPoint");}
Output
Run the code and check its 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
Take a look at the following example −
#include <stdio.h>intmain(){printf("%c",'\141');return0;}
Output
When you run this code, it will produce the following 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
Take a look at the following example −
#include <stdio.h>intmain(){printf("%c",'\x41');return0;}
Output
Here, you will get the following output −
A
Alert or Bell Number Escape Sequence (\a)
The escape sequence \a represents the alert or bell character. When executed, it produces a sound or visual alert depending on the terminal or console being used.
Example
Take a look at the following example −
#include <stdio.h>intmain(){printf("Hello \a world\n");return0;}
Output
Run the code and check its output −
Hello world
Escape sequences are used widely in many other programming languages such as Java, PHP, C#, etc.
Leave a Reply