Escape sequence is used to escape from the normal representation of the string. It is a combination of \ (Backslash) and some character.
As an example, \n
is used to move cursor to the new line and \r
is used to move the cursor to the beginning of the current line.
If you observe the below table, you can see a common character in escape sequence called backslash (\), it is called Escape Character. Other character is from C character set.
Table for Escape Sequence in C
Escape Sequence | Meaning | Purpose |
\b | Backspace | Move the cursor position to one character left on same line |
\n | new line | Move cursor to the next line |
\t | tab | Move cursor to the next horizontal tab position |
\r | Carriage return | Move cursor to the beginning of the current line |
\' | Single quote | Print the single quote |
\" | Double quote | Prints the double quote |
\\ | Backslash | Presents a character with backslash |
\0 | null | termination of character string |
\a | Alert | Alerts the user by making sound from speaker |
\f | Form feed | Move cursor to the initial position of next logical page. |
\t | Vertical tab | Move cursor to next vertical tab |
The program that we are experimenting now for escape sequence is given below
#include<stdio.h> int main() { printf("Programming9.com"); return 0; }
Output
Programming9.com
Let us see few examples for the Escape Sequences in C
Newline Escape sequence - \n
#include<stdio.h> int main() { printf("Programming\n9.com"); return 0; }
Output
Programming 9.com
Single quote and Double quote escape sequence - \" and \'
#include<stdio.h> int main() { printf("Progra\"mm\"ing\'9.com"); return 0; }
output
Progra"mm"ing'9.com
Tab Escape sequence in C
#include<stdio.h> int main() { printf("Programming\t9.com"); return 0; }
output
Programming 9.com
Backslash - \\
Backslash is the main escape character in c, however, we may sometimes need to print backslash in output.
#include<stdio.h> int main() { printf("Programming\\9.com"); return 0; }
output
Programming\9.com