Escape Sequences in C Language | Escape Characters in C
Introduction:
In our previous article, We looked at the Variables in C-Language. In this article We are going to discuss Escape Sequences in C programming language. We will learn what is mean by Escape sequences in C and how to use escape characters.
Escape Sequences in C:
Most of the characters C-Language are printable characters. But there are few characters which can’t be printed on to console.
The characters like New line, Tab, Backslash, etc can’t be printed directly. So to print these kind of non-printable characters we use the Escape sequences.
Escape sequences in programming is a special sequence of characters, which is used to escape the meaning of the sequence to give it a different meaning.
An Escape sequences in C Language always start with the backslash ( \ ) character and followed by one or more characters.
For Example the new line character in C printed by using '\n' Escape Character. This type characters refers to single sequence even though they are written in two characters.
Similarly, We use to print the tab, We will use the backslash '\' and character 't' together to form the escape sequence '\t' which will be used to represent the Tab.
List of Escape Sequences in C Language:
The Complete list of Escape Characters are Listed Below along with their ASCII value.
Character | Escape Sequence | ASCII Value |
---|---|---|
Back Space | \b | 08 |
Bell (Alert) | \a | 07 |
Horizontal Tab | \t | 09 |
New Line | \n | 10 |
Vertical Tab | \v | 11 |
Form Feed | \f | 12 |
Carriage Return | \r | 13 |
Quotation Mark | \" | 34 |
Apostrophe | \’ | 039 |
Backslash | \\ | 092 |
Now we have seen the list of escape characters, Let’s see how we can use them in program to get the desired output.
Examples to understand the Escape Sequences in C Programming:
New line ( “\n” ) Escape Character usage:
We use "\n" for new line character.
Program:
1 2 3 4 5 6 7 8 |
#include <stdio.h> int main() { // "\n" escape sequence for printing the New line printf("Hello \n"); printf("World \n"); return 0; } |
Output:
1 2 3 4 |
$ ./a.out Hello World $ |
As you can see the "\n" character bring the printing to new line.
Let’s see same example without "\n" escape sequence.
Program:
1 2 3 4 5 6 7 |
#include <stdio.h> int main() { printf("Hello"); printf("World"); return 0; } |
Output:
1 2 3 |
// No new line character is used $ ./a.out HelloWorld$ |
Example 2: Printing Quotes and Backslash in C Language:
As we discussed earlier we can’t directly print the quotes (") and backslash ("\") characters. So we need to use the backslash character to escape these letters.
Program:
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> int main() { // We are using "\" to escape // We are also use "\n" for new line. printf("Quotation Mark : \" \n"); printf("BackSlash : \\ \n"); return 0; } |
Output:
1 2 3 4 |
$ ./a.out Quotation Mark : " BackSlash : \ $ |
As you can see the backslash infront of the Quote and backSlash.
📢 To print the black-slash character on console, We need to use another back-slash character as escape character.
1 Response
[…] the previous article, We discussed about the Escape Sequences in C programming Language. In today’s article, We will learn about the different Datatypes in C […]