The escape sequence in C is the characters or the sequence of characters that can be used inside the string literal. The purpose of the escape sequence is to represent the characters that cannot be used normally using the keyboard. Some escape sequence characters are the part of ASCII charset but some are not.
Different escape sequences represent different characters but the output is dependent on the compiler you are using.
Escape Sequence List
The table below lists some common escape sequences in C language.
Escape Sequence |
Name |
Description |
\a |
Alarm or Beep |
It is used to generate a bell sound in the C program. |
\b |
Backspace |
It is used to move the cursor one place backward. |
\f |
Form Feed |
It is used to move the cursor to the start of the next logical page. |
\n |
New Line |
It moves the cursor to the start of the next line. |
\r |
Carriage Return |
It moves the cursor to the start of the current line. |
\t |
Horizontal Tab |
It inserts some whitespace to the left of the cursor and moves the cursor accordingly. |
\v |
Vertical Tab |
It is used to insert vertical space. |
\\ |
Backlash |
Use to insert backslash character. |
\’ |
Single Quote |
It is used to display a single quotation mark. |
\” |
Double Quote |
It is used to display double quotation marks. |
\? |
Question Mark |
It is used to display a question mark. |
\ooo |
Octal Number |
It is used to represent an octal number. |
\xhh |
Hexadecimal Number |
It represents the hexadecimal number. |
\0 |
NULL |
It represents the NULL character. |
Out of all these escape sequences, \n and \0 are used the most. In fact, escape sequences like \f, \a, are not even used by programmers nowadays.
Escape Sequence in C Examples
The following are the escape sequence examples that demonstrate how to use different escape sequences in C language.
1. Example to demonstrate how to use \a escape sequence in C
C
#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 7873923408
2. Example to demonstrate how to use \b escape sequence in C
C
#include <stdio.h>
int main( void )
{
printf ( "Hello \b\b\b\b\b\bHi Geeks" );
return (0);
}
|
Output
Hi Geeks
3. Example to demonstrate how to use \n escape sequence in C
C
#include <stdio.h>
int main( void )
{
printf ( "Hello\n" );
printf ( "GeeksforGeeks" );
return (0);
}
|
Output
Hello
GeeksforGeeks
4. Example to demonstrate how to use \t escape sequence in C
C
#include <stdio.h>
int main( void )
{
printf ( "Hello \t GFG" );
return (0);
}
|
The escape sequence “\t” is very frequently used in loop-based pattern printing programs.
5. Example to demonstrate how to use \v escape sequence in C
C
#include <stdio.h>
int main( void )
{
printf ( "Hello friends\v" );
printf ( "Welcome to GFG" );
return (0);
}
|
Output
Hello friends
Welcome to GFG
6. Example to demonstrate how to use \r escape sequence in C
C
#include <stdio.h>
int main( void )
{
printf ( "Hello Geeks \rGeeksfor" );
return (0);
}
|
Output
GeeksforGeeks
7. Example to demonstrate how to use \\ escape sequence in C
C
#include <stdio.h>
int main( void )
{
printf ( "Hello\\GFG" );
return (0);
}
|
Explanation: It contains two escape sequence means it after printing the \ the compiler read the next \ as a newline character i.e. \n, which print the GFG in the next line.
8. Example to demonstrate how to use \’ and \” escape sequence in C
C
#include <stdio.h>
int main( void )
{
printf ( "\' Hello Geeks\n" );
printf ( "\" Hello Geeks" );
return 0;
}
|
Output
' Hello Geeks
" Hello Geeks
9. Example to demonstrate how to use \? escape sequence in C
C
#include <stdio.h>
int main( void )
{
printf ( "\?\?!\n" );
return 0;
}
|
10. Example to demonstrate how to use \ooo escape sequence in C
C
#include <stdio.h>
int main( void )
{
char * s = "A\072\065" ;
printf ( "%s" , s);
return 0;
}
|
Explanation: Here 000 is one to three octal digits(0….7) means there must be at least one octal digit after \ and a maximum of three. Here 072 is the octal notation, first, it is converted to decimal notation which is the ASCII value of char ‘:’. At the place of \072, there is: and the output is A:5.
11. Example to demonstrate how to use \xhh escape sequence in C
C
#include <stdio.h>
int main( void )
{
char * s = "B\x4a" ;
printf ( "%s" , s);
return 0;
}
|
Explanation: Here hh is one or more hexadecimal digits(0….9, a…f, A…F). There can be more than one hexadecimal number after \x. Here, ‘\x4a’ is a hexadecimal number and it is a single char. Firstly it will get converted into decimal notation and it is the ASCII value of the char ‘J’. Therefore at the place of \x4a, we can write J. So the output is BJ.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Aug, 2023
Like Article
Save Article