Escape Sequences in C – Types, Syntax and Examples

C provides various types of features and functionalities to the programmers. C has a vast collection of standard library functions. Escape sequences are one of the most striking and useful features of the C programming language. Escape sequences provide different meanings to your program code. The predefined function of each escape sequence differs from one another.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

What is Escape Sequence in C?

From the name, you can easily state that in an escape sequence, a character denotes a different meaning from its actual meaning and undergoes a change from its normal form. Each Escape sequence is denoted by a backslash(‘\’).

It is a sequence of characters which is used for formatting the output in a structured and beautiful way. But it did not get displayed on the screen while printing the output.

For Example:-

char s = '\t';

In the above example, we have declared a variable s with the value ‘\t’. The ‘\t’ has a special meaning. It is used for inserting a horizontal tab. The compiler will interpret the character ‘t’ as an escape sequence because the character ‘t’ is preceded by a backslash(‘\’).

Significance of C Escape Sequence

Let’s take an example to understand the significance of the escape sequence in C.

Suppose, you want to print some new lines on your program code for enhancing the code readability. For that, you have to write multiple white spaces in the printf() function. And it is not an appropriate solution.

You can easily solve this type of problem using the ‘\n’ escape sequence. About 15 different types of escape sequences are present in the C programming language.

Example of Escape Sequence using ‘\n’

#include<stdio.h>
int main()
{
printf("TechVidvan\n");
printf("Tutorial\n");
printf("Escape Sequences\n");
return 0;
}

Output:-

TechVidvan
Tutorial
Escape Sequences

Types of Escape Sequence in C

Below are the types of Escape Sequences:-

  • \n (New line):- Used to print a new line.
  • \t (Horizontal tab):- Used to print a horizontal tab.
  • \a (Audible bell):- Used for alarm and beep.
  • \r (Carriage Return):- Used for inserting a carriage return in the text at this point.
  • \\ (Backslash):- Used for inserting a backslash character in the text at this point.
  • \’ (Apostrophe or single quotation mark):- Used for displaying a single quotation mark on the screen.
  • \” (Double quotation mark):- Used for displaying a double quotation mark on the screen.
  • \0 (Null character):- Used to represent a null character.
  • \? (Question mark):- Mainly used for displaying a question mark on the screen.
  • \nnn (Octal number):- Used to represent octal number.
  • \xhh (Hexadecimal number):-Used to represent a hexadecimal number.
  • \v:- Used for a vertical tab.
  • \b:- Used for backspace.
  • \e:- Used for escape characters.
  • \f:- used for Form Feed page break.

1. New Line(‘\n’) in C

Used for creating a new line on the program code.

Example of New Line

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: New Line!");
printf("\n"); // creating a new line!
printf ("Have a great day!\n");// creating a new line!
printf("Thank You!");
return 0;
}

Output:-

TechVidvan Tutorial: New Line!
Have a great day!
Thank You!

2. Horizontal Tab(‘\t’) in C

If you use ‘\t’ in your program then the words which will come after ‘\t’ will be inserted into the same line leaving some spaces.

Example:- Horizontal Tab

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Horizontal Tab!\n");
printf("45\t693\t145\t526");
return 0;
}

Output:-

TechVidvan Tutorial: Horizontal Tab!
45 693 145 526

3. Backspace(‘\b’) in C

Word which precedes the ‘\b’ will be removed.

Example:- Backspace

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutoriall\b: Backspace!\n"); //we used '\b' to remove the extra l!
return 0;
}

Output:-

TechVidvan Tutorial: Backspace!

4. Carriage Return(‘\r’) in C

Used for inserting a carriage return in the text at this point.

Example:- Carriage Return

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Carriage Return!\n");
printf("Just an example!");
printf(" \r NoT JUst");
return 0;
}

Output:-

TechVidvan Tutorial: Carriage Return!
NoT JUstxample!

5. Audible Bell(‘\a’) in C

Used for generating a bell sound when the execution of the program ends.

Example:- Audible Bell

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Audible Bell!\n"); 
printf("Bell Ringing!\a"); // rings a bell
return 0;
}

Output:-

TechVidvan Tutorial: Audible Bell!
Bell Ringing!

6. Single Quotation(\’) in C

Used to print the single quotation mark on the screen.

Example:-Single Quotation

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Single Quotation!\n");
printf("'Have a great day!\'");
return 0;
}

Output:-

TechVidvan Tutorial: Single Quotation!
‘Have a great day!’

7. Double Quotation(\”) in C

Used to print the double quotation mark on the screen.
Example:- Double Quotation

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Double Quotation!\n");
printf("\"Have a great day!\"");
return 0;
}

Output:-

TechVidvan Tutorial: Double Quotation!
“Have a great day!”

8. Question Mark(\?) in C

Used to print the quotation mark on the screen.

Example:- Question Mark

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Question Mark!\n");
printf("Are you having a great day\?");
return 0;
}

Output:-

TechVidvan Tutorial: Question Mark!
Are you having a great day?

9. Backslash(\\) in C

Used to print the backslash(\) on the screen.

Example:- Backslash

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Backslash!\n");
printf("C:\\Users\\TechVidvan");
return 0;
}

Output:-

TechVidvan Tutorial: Backslash!
C:\Users\TechVidvan

10. Form Feed(\f) in C

This escape sequence is used for a form feed.

Example:- Form Feed

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Form Feed!\n");
printf("How\fYou\fdoing!");
return 0;
}

Output:-

TechVidvan Tutorial: Form Feed!
How
You
doing!

11. Vertical Tab(\v) in C

The purpose of this is to print a vertical tab.

Example:- Vertical Tab

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Vertical Tab!\n");
printf("Hi,\vJohn\vhere!");
return 0;
}

Output:-

TechVidvan Tutorial: Vertical Tab!
Hi,
John
here!

12. Null Value(\0) in C

This is used to print null value.

Example:- Null Value

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Null Value!\n");
printf("Just null values! \0 gone \0 gone");
return 0;
}

Output:-

TechVidvan Tutorial: Null Value!
Just null values!

13. Octal Value(\nnn) in C

This is used to print the octal value equivalent to the character.

Example:- Octal Value

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Octal Value!\n");
char* o = "B\141";
printf("%s",o);
return 0;
}

Output:-

TechVidvan Tutorial: Octal Value!
Ba

14. Hexadecimal Value(\xhh) in c

Used to print the hexadecimal value.

Example:- Hexadecimal Value

#include <stdio.h>
int main ()
{
printf("TechVidvan Tutorial: Hexadecimal Value!\n");
char* o = "B\x2s";
printf("%s",o);
return 0;
}

Output:-

TechVidvan Tutorial: Hexadecimal Value!
Bs

Example of Escape Sequences

#include <stdio.h>
int main()
{
printf("TechVidvan Tutorial: Escape Sequences!\n\n");
printf("Just a new\nline!\n");// newline!
printf("\'Welcome Back\'!\n"); // single quotation!
printf("22\v25\n"); // vertical tab!
printf("\"Escape Sequences!\"\n"); // double quotation!
printf("Hi!");
printf("\r"); // carriage return!
printf("Just an example!\n");
printf("How are you\?\n"); // question mark!
printf("D:\\TechVidvan\\C\n"); // backslash!
// Octal Value!
char* o = "B\142";
printf("%s\n", o);
// Octal Value!
// Hexadecimal Value
char* T = "B\x2L";
printf("%s\n", T);
// Hexadecimal Value!
return 0;
}

Output:-

TechVidvan Tutorial: Escape Sequences!

Just a new
line!
‘Welcome Back’!
22
25
“Escape Sequences!”
Just an example!
How are you?
D:\TechVidvan\C
Bb
BL

Summary

In this tutorial, we learnt about the escape sequences in C. We also discussed the types of escape sequences present in C. After that we discussed each escape sequence in detail followed by their examples. Escape sequences are very useful to such programmers who want to format the output.

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.