ITDeveloper : C Programming
C Programming
Step by Step Tutorials
![]() Share with a Friend |
Escape Sequence In C
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.
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.
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.
Here is a simple code in C which illustrates the use of \n escape sequence:
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:
Escape Sequence |
Meaning |
\n |
New Line |
\t |
Horizontal Tab |
\b |
BackSpace |
\r |
Carriage Return |
\a |
Audible Bell |
\’ |
Printing Single Quotation |
\” |
Printing Double Quotation |
\? |
Question Mark Sequence |
\ |
Back Slash |
\f |
Form Feed |
\v |
Vertical Tab |
\0 |
Null Value |
\nnn |
Print Octal Value |
\xhh |
Print Hexadecimal Value |
1. \n (New Line)
It is used to create a new line and place the cursor there. Words that come after ‘\n’ will be pushed to a new line. Its ASCII value is 010.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n New Line Escape Sequence : ");
printf("\n First Line");
printf ("\n Second Line \n");
return 0;
}
Output:
New Line Escape Sequence : First Line
Second Line
2. \t (Horizontal Tab)
This is the escape sequence for the horizontal tab. Words that come after ‘\t’ will be pushed in the same line leaving some spaces. Its ASCII value is 009.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Horizontal Tab Escape Sequence : ");
printf(" \n 34543 \t 345435 ");
printf(" \n 123 \t 678 ");
return 0;
}
Output:
Horizontal Tab Escape Sequence :
34543 345435
123 678
3. \b (BackSpace)
This is the escape sequence for backspace. A word that precedes \b’ will be removed. Its ASCII value is 008.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Backspace Escape Sequence : ");
printf(" \n Watch\b carefully the execution");
return 0;
}
Output:
Backspace Escape Sequence :
Watc carefully the execution
4. \r (Carriage Return)
This is the escape sequence to position the cursor at the beginning of the line. Its ASCII value is 013.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n demo code below");
printf(" \r remove");
printf("\n done with example");
return 0;
}
Output:
removeode below
done with example
5. \a (Audible bell)
This is the escape sequence to generate a bell sound to denote the execution of the program. Its ASCII value is 013. Its ASCII value is 007.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Here is the example");
printf(" \n Bell Sound\a");
return 0;
}
Output:
Here is the example
Bell Sound
6. \’ (Printing single quotation)
This escape sequence is used to print the single quotation mark. Its ASCII value is 039.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Hello !!! Welcome to ITDeveloper ' C Programming Tutorial");
printf(" \n\tooth paste for clean teeth' ");
return 0;
}
Output:
Hello !!! Welcome to ITDeveloper ' C Programming Tutorial
ooth paste for clean teeth'
7. \” (printing double quotation)
This escape sequence is used to print the single quotation mark. Its ASCII value is 034.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n This is the example ");
printf(" \n\"baba blacksheep example\" ");
printf(" \n\"double quotes surrounded text\" ");
return 0;
}
Output:
This is the example
"baba blacksheep example"
"double quotes surrounded text"
8. \? (Question Mark Sequence)
This escape sequence is used to print the question mark(?). Its ASCII value is 063.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n This is the example");
printf(" \n What’s the price of the pen drive \? ");
printf(" \n What’s your father’s name\? ");
return 0;
}
Output:
This is the example
What's the price of the pen drive ?
What’s your father’s name?
9. \\ (Back Slash)
This escape sequence is used to print the backslash (\). Its ASCII value is 092.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Example of the Escape Sequence");
printf(" \n C:\\folder\\folder1\\folder2");
printf(" \n D:\\folder\\folder1\\folder2 ");
printf(" \n E:\\folder\\folder1\\folder2 ");
printf(" \n F:\\folder\\folder1\\folder2 ");
return 0;
}
Output:
Example of the Escape Sequence :
C:\folder\folder1\folder2
D:\folder\folder1\folder2
E:\folder\folder1\folder2
F:\folder\folder1\folder2
10. \f (Form Feed)
This escape sequence is used for a form feed. Its ASCII value is 012.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Below is a Classic Example");
printf(" \n \f ");
return 0;
}
Output:
Below is a Classic Example
11. \v (Vertical Tab)
This is used to print the vertical tab. Its ASCII Value is 011.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Example of Vertical Tab Escape Sequence");
printf(" \n \v Charlie \t Chaplin ");
return 0;
}
Output:
Example of Vertical Tab Escape Sequence
Charlie Chaplin
12. \0 (Null Value)
This is used to print null value. Its ASCII value is 000. The statement after \0 will be omitted.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Learning the Null Value ");
printf(" \n Python \0 Tutorial ");
return 0;
}
Output:
Learning the Null Value
Python
This is used to print the octal value equivalent character.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Below is the example of printing Octal Value");
char* b = "B\124";
printf(" \n%s", b);
return 0;
}
Output:
Below is the example of printing Octal Value
BT
14. \xhh(Print Hexadecimal value)
This sequence is used to print the hexadecimal value.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n Formatting output for Hexadecimal Value");
char* s = "B\x5b";
printf("\n %s", s);
return 0;
}
Output:
Formatting output for Hexadecimal Value
B[
Consolidated Example
Input:
#include <stdio.h>
int main()
{
printf("ITDeveloper\Tutorial \n");
printf("New Line \n Next Line \n");
printf("welcome 'to' consolidated\? \v example \n");
printf("\v");
printf("\"learning is fun\" ");
printf("\r");
printf(" \n'text surrounded with single quotation' ");
printf(" \n\"double quotes surrounded text\" ");
printf(" \n whats your fathers name\? ");
printf(" \n E:\folder\folder1\folder2 ");
char* b = "B\124";
printf(" \n%s", b);
char* s = "B\x5b";
printf("\n %s", s);
return 0;
}
Output:
ITDeveloperTutorial New Line New Line welcome 'to' consolidated? example learning is fun 'text surrounded with single quotation' "double quotes surrounded text" whats your fathers name? E:\folder\folder1\folder2 BT B[
Example
Code:
int main()
{
printf("Example Program \n");
printf("Welcome To \n new line \n");
printf("have you\? \v had breakfast \n");
printf("\v");
printf("\"test\" ");
printf("\r");
return 0;
}
Output:
Example Program Welcome To new line have you? had breakfast "test"