Escape Sequences in C Programming [Complete List]

What is Escape Sequence?

An escape sequence in C is a sequence of characters or we can say that it is a combination of two or more characters that do not represent itself when used inside a character or string literal but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.

List of all important Escape Sequences in C programming

\a : Alarm or Beep   
\b : Backspace
\f  : Form Feed
\n : New Line
\r  : Carriage Return
\t  : Tab (Horizontal)
\v : Vertical Tab
\\  : Backslash
\'  : Single Quote
\" : Double Quote
\?:Question Mark
\ooo : octal number
\xhh : hexadecimal number
\0 : Null

You can see the above list that we use in C programs. I already highlighted the most useful escape sequences in C.

Let’s understand highlighted escape sequences with one example-

\n for a new line

You can use “\n” to insert a new line before or after any line. It can be used with printf(), scanf() functions easily.

#include<stdio.h>
#include<conio.h>

void main()
{
clrscr();
printf("\n");
printf("Hello World\n");
printf("\n\nMy name is Ashish");
getch();
}

Output

1. ---blank line---
2.Hello World
3. ---blank line--- 
4. ---blank line--- 
5. ---blank line--- 
6.My name is Ashish

You can see the above code, I write 3 different print statements. The first statement will insert a new line. The second statement will print “Hello World” and then insert a new line. The last line will insert 2 new lines and print “My name is Ashish”.

\t for a tab

You can use “\t” for tab between two words or values. It is approx 8 spaces. It can be used with printf() and scanf() functions.

#include<stdio.h>
#include<conio.h>

void main()
{
printf("Hello \tWorld");
getch();
}

Output

Hello         World

It is used to format the output/result of any program. Mostly it is used to show the result of structured data.

\’ for a single quote

When we display the output of any program and needs to print a single quote word. e.g. “You’re the best teacher” etc.

So, how you print the above statement?

#include<stdio.h>
#include<conio.h>

void main()
{
clrscr();
printf("You\'re the best teacher!");
getch();
}

Output

You're the best teacher!

Similarly, you can use double quote. Just replace the single quote syntax with double-quote syntax. That’s it.

You can also read

Hope that you understand the escape sequence in C. Please ask your questions in below comment box and don’t forget to share this post on your social profile or your friends.

If you love blogging and want to learn please visit our other blog https://zerootech.com

Leave a Comment

error: Don\'t make piracy - Write your own content :)