Updated September 25, 2023
Introduction to Escape Sequence in C
As the name denotes, the escape sequence denotes the scenario in which a character undergoes a change from its normal form and denotes something that is different than its usual meaning. Generally, an escape sequence begins with a backslash ‘\’ followed by a character or characters. The c compiler interprets any character followed by a ‘\’ as an escape sequence. Escape sequences are used to format the output text and are not generally displayed on the screen. Each escape sequence has its own predefined function.
Examples of Escape Sequence in C
The following are the examples of an escape sequence.
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 tutorial");
printf("\n first line");
printf ("\n Second line \n");
return 0;
}
Output:
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 tutorial");
printf(" \n 34543 \t 345435 ");
printf(" \n 123 \t 678 ");
return 0;
}
Output:
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 tutorial");
printf(" \n watch\b carefully the execution");
return 0;
}
Output:
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:
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 demo ");
printf(" \n bell sound\a");
return 0;
}
Output:
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 mam maesafm sadsdm ' sdsa asdsadas tutorial");
printf(" \n\tutu tutu du dutut tututu tutut\' ");
return 0;
}
Output:
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 here is a demo ");
printf(" \n\"baba blacksheep example\" ");
printf(" \n\"double quotes surrounded text\" ");
return 0;
}
Output:
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 below is the demo");
printf(" \n what’s the price of one kg of tomatoes \? ");
printf(" \n what’s your father’s name\? ");
return 0;
}
Output:
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 following are the usage of escape sequence");
printf(" \n C:\\test\\test1\\test2");
printf(" \n D:\\test\\test1\\test2 ");
printf(" \n E:\\test\\test1\\test2 ");
printf(" \n F:\\test\\test1\\test2 ");
return 0;
}
Output:
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:
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 usgae of vertical tab escape sequence");
printf(" \n \v vignesh \t krishnakumar ");
return 0;
}
Output:
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 ooculussss \0 reparo ");
return 0;
}
Output:
13. \nnn (Print octal value)
This is used to print the octal value equivalent character.
Example
Code:
#include <stdio.h>
int main ()
{
printf("\n below is the demo of printing octal value");
char* b = "B\124";
printf(" \n%s", b);
return 0;
}
Output:
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:
Consolidated Example
Input:
#include <stdio.h>
int main()
{
printf("vignesh\krishnakumar \n");
printf("new line \n next line \n");
printf("welcome \'to\' concolidated\? \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:\\test\\test1\\test2 ");
char* b = "B\124";
printf(" \n%s", b);
char* s = "B\x5b";
printf("\n %s", s);
return 0;
}
Output:
Example
Code:
int main()
{
printf("Example Program \n");
printf("Welcome To \n new line \n");
printf("have you\? \v had brekafast \n");
printf("\v");
printf("\"test\" ");
printf("\r");
return 0;
}
Output:
Conclusion
Thus, the article covered in detail about the various escape sequences available in c. Also, the article covered the various escape sequences by explaining each with appropriate examples. It would be wise to learn and practice more about escape sequences as the output needs to be formatted always. To learn in-depth about the escape sequences it is advisable to create sample programs and have fun working around them.
Recommended Articles
This is a guide to Escape Sequence in C. Here we discuss the Introduction and Escape Sequence in C with Examples which includes,\n (New Line),\nnn (Print octal value), etc. You may also look at the following articles to learn more –