EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Escape Sequence in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Escape Sequence in C

Escape sequence in C

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Example Escape Sequence is C

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:

Escape Sequence is C-1.1

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:

Escape Sequence is C-1.2

3. \b (BackSpace)

This is the escape sequence for backspace. A word that precedes \b’ will be removed. Its ASCII value is 008.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)
Example:

Code:

#include <stdio.h>
int main ()
{
printf("\n backspace escape sequence tutorial");
printf(" \n watch\b carefully the execution");
return 0;
}

Output:

Escape Sequence is C-1.3

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:

Escape Sequence is C-1.4

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:

Escape Sequence is C-1.5

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:

Escape Sequence is C-1.6

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:

Escape Sequence is C-1.7

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:

Escape Sequence is C-1.8

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:

Escape Sequence is C-1.9

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:

Escape Sequence is C-1.10

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:

Output-1.11

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:

Output-1.12

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:

Escape Sequence is C-1.13

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:

Output-1.14

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:

Output-1.15

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:

Output-1.16

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 –

  1. C Commands
  2. Nested Structure in C
  3. Arithmetic Operators in C
  4. C vs C++

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More