15 Types of Escape Sequence in C that Make your Coding Better

An Escape Sequence in C is a sequence of characters that doesn’t represent itself when used inside a character or a string literal but has its own specific function. 

So far we have learned the character set along with Tokens in C in detail but we haven’t come across the term “Escape sequence”.

In order to unveil this topic, let us begin step by step its detailed discussion. The concept of escape sequences was originally developed in the C language, and then, later on, carried to other languages like C# and Java.

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

What is Escape Sequence in C?

An escape sequence is a sequence of characters used in formatting the output and are not displayed while printing text on to the screen, each having its own specific function.

All the escape sequences in C are represented by 2 or more characters, one compulsorily being backslash (\) and the other any character present in the C character set.

Escape Sequence in C Language

Significance of Escape Sequence in C

In order to acknowledge the significance of escape sequences, let us consider a simple problem at hand where we want to display some text in a new line in order to enhance code readability. A novice at C programming would say that this task can be ambiguously achieved by placing multiple white spaces in the printf() function.

This is not an appropriate method to solve this problem, as it proves to be quite inconvenient and requires multiple test cases to get the output according to the user’s wish. We can solve this problem by using \n escape sequence. Not only this, the C language offers about 15 escape sequences that allow the user to format the output on the screen.

Before we move ahead let’s revise the concept of Functions in C

Here is a simple code in C which illustrates the use of \n escape sequence:

#include<stdio.h>
int main()
{

printf("Welcome\n");
printf("to\n");
printf("DataFlair\n");
printf("tutorials!\n");
return 0;
}

Code on Screen-

Example of n escape sequence in C

Output

Output of n escape sequence in C

Types of Escape Sequence in C

There are 15 types of escape sequence in C to achieve various purposes.

Here is a table which illustrates the use of escape sequences in C:

  1. \n (New line) – We use it to shift the cursor control to the new line
  2. \t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line.
  3. \a (Audible bell) – A beep is generated indicating the execution of the program to alert the user.
  4. \r (Carriage Return) – We use it to position the cursor to the beginning of the current line.
  5. \\ (Backslash) – We use it to display the backslash character.
  6. \’ (Apostrophe or single quotation mark) – We use it to display the single-quotation mark.
  7. \” (Double quotation mark)- We use it to display the double-quotation mark.
  8. \0 (Null character) – We use it to represent the termination of the string.
  9. \? (Question mark) – We use it to display the question mark. (?)
  10. \nnn (Octal number)- We use it to represent an octal number.
  11. \xhh (Hexadecimal number) – We use it to represent a hexadecimal number.
  12. \v (Vertical tab)
  13. \b (Backspace)
  14. \e (Escape character)
  15. \f (Form Feed page break)

Get the Samurai Technique to Learn Arrays in C

Here is a code in C that illustrates the commonly used escape sequences in C:

#include<stdio.h>
int main()
{
/* To illustrate the use of \n escape sequence */

printf("Welcome\v");
printf("to\n");
printf("DataFlair\v");
printf("tutorials!\n");

/* To illustrate the use of \n escape sequence */

printf("Welcome\tto\tDataFlair\ttutorials!");

/* To illustrate the use of \v escape sequence */

printf("Welcome\vto\vDataFlair\vtutorials!");
return 0;
}

Code on Screen-

Escape sequences in C

Output-

Output of escape sequences in C:

Summary

In this tutorial, we discussed the important escape sequences in C along with their significance in the C programming language. Further, we discussed the different types of escape sequences in detail and an illustrative program that helped us understand its use.

After reading all this, I recommend you to check Strings in C Language.

Don’t forget to provide us your precious review. Leave your comments below, if you are having any further queries.

1 Response

  1. Imran khan says:

    This is one of the best site for c language or c++ it’s really very helpful 😍 ❤ 😊 it’s help me alot i just love this site ❤ i recommend everyone for this site you did a wonderful job 🙌🏻🙏👌🏻👍👍👏👏🎆🎇✨🎈🎉🎊

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.