Escape sequences in C

In C programming language, there are 256 quantities of characters in character set. The whole character set is separated into 2 sections for example the ASCII characters set and the all-inclusive ASCII characters set. Be that as it may, aside from that, some different characters are likewise there which are not the piece of any characters set, known as ESCAPE characters.

List of Escape Sequences

\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

Some coding examples of escape characters

// C program to illustrate 
// \a escape sequence 
#include <stdio.h> 
int main(void) 
{ 
	printf("My mobile number "
		"is 7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a"); 
	return (0); 
} 

Output:

My mobile number is 1234567890.
 
// C program to illustrate 
// \b escape sequence 
#include <stdio.h> 
int main(void) 
{ 
	// \b - backspace character transfers 
	// the cursor one character back with 
	// or without deleting on different 
	// compilers. 
	printf("Hello wp\b\b\b\bF"); 
	return (0); 
} 

Output:

The output is dependent upon compiler.
 
// C program to illustrate 
// \n escape sequence 
#include <stdio.h> 
int main(void) 
{ 
	// Here we are using \n, which 
	// is a new line character. 
	printf("Hello\n"); 
	printf("wp"); 
	return (0); 
} 

Output:

Hello
wp
// C program to illustrate 
// \t escape sequence 
#include <stdio.h> 
int
main(void) 
{ 
	// Here we are using \t, which is 
	// a horizontal tab character. 
	// It will provide a tab space 
	// between two words. 
	printf("Hello \t w"); 
	return (0); 
} 

Output:

Hello   w

The escape sequence “\t” is very frequently used in loop based pattern printing programs.

// C program to illustrate 
// \v escape sequence 
#include <stdio.h> 
int main(void) 
{ 
	// Here we are using \v, which 
	// is vertical tab character. 
	printf("Hello friends"); 

	printf("\v Welcome to w"); 

	return (0); 
} 

Output:

Hello Friends
Welcome to w
// C program to illustrate \r escape 
// sequence 
#include <stdio.h> 
int main(void) 
{ 
	// Here we are using \r, which 
	// is carriage return character. 
	printf("Hello fri \r ends"); 
	return (0); 
} 

Output: (Depend upon compiler)

ends 
// C program to illustrate \\(Backslash) 
// escape sequence to print backslash. 
#include <stdio.h> 
int main(void) 
{ 
	// Here we are using \, 
	// It contains two escape sequence 
	// means \ and \n. 
	printf("Hello\\w"); 
	return (0); 
} 

Output: (Depend upon compiler)

Hello\w

Clarification : It contains two getaway succession implies it subsequent to printing the \ the compiler read the following \ as new line character for example \n, which print the W in the following line

// C program to illustrate \' escape 
// sequence/ and \" escape sequence to 
// print single quote and double quote. 
#include <stdio.h> 
int main(void) 
{ 
	printf("\' Hello w\n"); 
	printf("\" Hello w"); 
	return 0; 
} 

Output:

' Hello w
" Hello w
// C program to illustrate 
// \? escape sequence 
#include <stdio.h> 
int main(void) 
{ 
	// Here we are using \?, which is 
	// used for the presentation of trigraph 
	// in the early of C programming. But 
	// now we don't have any use of it. 
	printf("\?\?!\n"); 
	return 0; 
} 

Output:

??!
// C program to illustrate \OOO escape sequence 
#include <stdio.h> 
int main(void) 
{ 
	// we are using \OOO escape sequence, here 
	// each O in "OOO" is one to three octal 
	// digits(0....7). 
	char* s = "A\0725"; 
	printf("%s", s); 
	return 0; 
} 

Output:

A:5

Clarification : Here 000 is one to three octal digits(0… .7) implies there must be atleast one octal digit after \ and most extreme three.Here 072 is the octal documentation, first it is changed over to decimal documentation that is the ASCII estimation of scorch ‘:’. At the spot of \072 there is : and the yield is A:5.

// C program to illustrate \XHH escape 
// sequence 
#include <stdio.h> 
int main(void) 
{ 
	// We are using \xhh escape sequence. 
	// Here hh is one or more hexadecimal 
	// digits(0....9, a...f, A...F). 
	char* s = "B\x4a"; 
	printf("%s", s); 
	return 0; 
} 

You May Also Like

Abdul Vakeel

About the Author: Abdul Vakeel

WPBeginner Blog is a Free WordPress Resource Site For Beginners. WPBeginner Blog Was Founded in 4th June 2018 by Abdul Vakeel.The Main Goal of This Site is to Provide Quality Tips, Tricks, Hacks, and Other WordPress Resources That Allows WordPress Beginners to Improve Their Site.

Leave a Reply

Your email address will not be published. Required fields are marked *