What are Escape Sequences in C Language?
To modify the format of the output in C language, escape sequences are used. These characters are not displayed in the output. This character always begins with backslash “\”. It is known as an escape character.
Different Escape sequences in C language
Different escape sequences used in C language are as follows:
Escape sequence | purpose |
\b | Backspace |
\f | Form feed |
\n | New line |
\r | Carriage return |
\t | Tab |
\’ | Single quote |
\” | Double quote |
\xdd | ASCII code in hexadecimal notation. Each d represents a digit. |
\ddd | ASCII code in octal notation. Each d represents a digit. |
\b in C language
This escape sequence is used to insert backspace in the output. An example of this escape sequence is as follows:
Printf(“Hello\bWorld”);
The above example will display HelloWorld. First of all, Hello is printed but /b deletes the letter o. The World is printed. So “HelloWorld” is displayed on the screen.
\f in C language
This escape sequence is used to insert a blank paper into the printed output. It is called form feed. An example of this escape sequence is as follows:
Printf(“Hello\fWorld”);
The above example will display HelloWorld. After printing Hello, it will include a blank of paper and then print the rest of the output. It is used during printing.
\n in C language
This escape sequence is used to insert a new line in the output. An example of this escape sequence is as follows:
Printf(“Hello\nWorld”)
The above example will display Hello in one line and World in the next line. First of all, Hello is printed, and \n moves the cursor to the next line Then World is printed.
\r in C language
This escape sequence is used to move the cursor at the beginning of the current line. It is known as a carriage return. An example of this escape sequence is as follows
printf (“Hello\rWorld”);
The above example will display World. First of all. Hello is printed but \r moves the cursor at the beginning of the current line. The World is printed that overwrites Hello. So only World is displayed on the screen.
\t in C language
This escape sequence is used to insert a TAB in the output. An example of this escape sequence is as follows:
Printf (“Hello\tWorld”).
The above example will display Hello World. First of all, Hello is printed and \t inserts a TAB. Then the World is printed.
\‘ in C language
This escape sequence is used to display single quotes in the output. An example of this escape sequence is as follows:
Printf(“\’Hello World\’ “);
The above example will display “Hello World”
\” in C language
In this escape, sequence double quotes are displayed in the output. An example of this escape sequence is as follows:
Printf(“\”Hello World\” “);
The above example will display “Hello World”
\\ in C language
To display the backslash (\) in the output, \\ is used. An example of this escape sequence is as follows:
printf (“C:||”);
The above example will display C:\.
Frequently Asked Questions (FAQs)
What are escape sequences?
An escape sequence is an arbitrary combination of characters that doesn’t represent itself when used within a string literal but is instead replaced with another character or sequence of characters. For example, \n represents a newline character, whereas “\\” represents a single backslash.
What is \r in C language?
It is known as a carriage return. It is used to move the cursor at the beginning of the current line.
What is the use of \ t escape sequence?
In the C program, this sequence is used to insert the TAB in the output.
Example: printf(“Hello\tWorld”)
Output: Hello World
Leave a Reply