Escape Sequence in C

Escape Sequence in C

An escape sequence in C language is a sequence of characters that do not represent themselves when used inside a string literal or character.

What is the Escape Character?

We cannot print a new line by directly pressing the entry key in any programming language (C, C ++, Java, and C #). So the backslash here is called the “escape character”.

Using the escape character and some other character combinations, we use to print some impossible characters. In C, all escape sequences have two or more characters which is a combination of backslash and characters.

It is made up of two or more letters that begin with a backslash. For example: /n represents the new line.

List of Escape Sequences in C

Escape SequenceMeaning
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Liner Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\’ Single Quote
\”Double Quote
\?Question Mark
\nnnoctal number
\xhh hexadecimal number
\0Null
Example of Escape Sequence in C
#include<stdio.h>    
int main(){       
  int number=50;     
 printf("You \n are \n learning \n \'c\' language \n \"Do you know C language\" ");  
   return 0; 
}     

C Escape Sequences Facts

  • An escape sequence contains non-printing characters.
  • Escape sequences are special characters associated with (backlash).
  • An escape sequence always starts with a (backslash) and is followed by a character.
  • Escape sequences are mostly used in printf() statements.
Examples of Escape Sequence in C
Escape sequence in C programming language

Following are examples of migration sequence.

1. \n (New Line)

It is use to create a new line and place the cursor there. The words that come after ‘\n’ will be pushed into a new line. Its ASCII value is 010.

Example:
#include <stdio.h>
int main (){
printf("\n new line escape sequence tutorial");
printf("\n first line");
printf ("\n Second line n\");
return 0;
}
2. \t (Horizontal Tab)

This escape sequence is use for the horizontal tab. The words that come after ‘\t’ will be pushed in the same line, leaving some places. Its ASCII value is 009.

Example:
#include <stdio.h>
int main (){
printf("\n horizontal tab escape sequence tutorial");
printf(" \n 34543 \t 345435 ");
printf(" \n 123 \t 576 ");
return 0;
}
3. \b (BackSpace)

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

Example:
#include <stdio.h>
int main (){
printf("\n backspace escape sequence tutorial for you");
printf(" \n watch \b the execution peacefully and carefully ");
return 0;
}
4. \r (Carriage Return)

This is the sequence of escape from the cursor position at the beginning of the line. Its ASCII value is 013.

Example:
#include <stdio.h>
int main (){
printf("\ n demo code is given below");
printf(" \r remove");
printf("\n  done with example");
return 0;
}
5. \a (Audible bell)

This escape sequence is use to generate a bell sound to denote the execution of the program. Its ASCII value is 013. Its ASCII value is 007.

Example:
#include <stdio.h>
int main (){
printf("\n the demo is given below ");
printf("\n bell sound \a");
return 0;
}
6. \’ (Printing single quotation)

This escape sequence is used to print a single quotation mark. Its ASCII value is 039.

Example:
#include <stdio.h>
int main (){
printf("\n Let\'s learn with this tutorial");
printf(" \n This is Harry\'s bag");
return 0;
}
7. \” (printing double quotation)

This escape sequence is used to print a single quotation mark. Its ASCII value is 034.

Example:
#include <stdio.h>
int main (){
printf("\n demo is given below ");
printf(" \n \" Hey Welcome guys. Glad to see you again.\" ");
printf(" \n \" Dear, please have a coffee with me\" ");
return 0;
}
8. \? (Question Mark Sequence)

This escape sequence is use for printing the question mark (?). Its ASCII value is 063.

Example:
#include <stdio.h>
int main (){
printf("\n Given below is the demo");
printf(" \n what\'s the price of one kg of onions \? ");
printf(" \n what\'s your father\'s name\? ");
return 0;
}
9. \\ (Back Slash)

This escape sequence is used to print backslash (). Its ASCII value is 092.

Example:
#include <stdio.h>
int main (){
printf("\n These are the following 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;
}
10. \f (Form Feed)

This escape sequence is used for form feeds. Its ASCII value is 012.

Example:
#include <stdio.h>
int main (){
printf("\n below is a classic example");
printf("\n \f ");
return 0;
}
11. \v (Vertical Tab)

It is used to print vertical tabs. Its ASCII value is 011.

Example:
#include <stdio.h>
int main (){
printf("\n usage of vertical tab escape sequence");
printf(" \n \v Haresh \t Manish kumar ");
return 0;
}

12. \0 (Null Value)

It is used to print a null value. Its ASCII value is 000. Subsequent statements will be omitted.

Example:
#include <stdio.h>
int main (){
printf("\n learning about the null value ");
printf(" \n some \0  words");
return 0;
}
13. \nnn (Print octal value)

It is used to print a character equal to the octal value.

Example:
#include <stdio.h>
int main (){
printf("\n  the demo is given below to print octal value");
char* b = "B124";
printf(" \n%s", b);
return 0;
}

14. \xhh(Print Hexadecimal value)

This sequence is used to print a hexadecimal value.

Example:
#include <stdio.h>
int main (){
printf("\n Formatting output for hexadecimal value");
char* s = "Bx5b";
printf("\n %s", s);
return 0;
}
Consolidated Example:
#include <stdio.h>
int main(){
printf("Haresh\Manish kumar \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;
}
Example:
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;
}

Conclusion

Thus, the article covers in detail the various escape sequences available in C.

Also, the article covered various escape sequences explaining each example. It would be wise to practice more about learning and escape sequences because the output always needs to be formatted.

To get an in-depth knowledge of escape sequences, it is worthwhile to create a sample program and enjoy working around them.

Leave a Comment