Escape Sequence in C
Escape Sequence in C
Escape Sequence in C
An escape sequence in C is a literal made up of more than one character put inside
single quotes. Normally, a character literal consists of only a single character inside
single quotes. However, the escape sequence attaches a special meaning to the
character that appears after a backslash character (\).
The \ symbol causes the compiler to escape out of the string and provide meaning
attached to the character following it.
Hello
World
\\ \ character
https://www.tutorialspoint.com/cprogramming/c_escape_sequences.htm 1/7
6/16/24, 11:02 AM Escape Sequence in C
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
Let us understand how these escape sequences work with the help of a set of
examples.
Example
#include <stdio.h>
int main(){
Output
https://www.tutorialspoint.com/cprogramming/c_escape_sequences.htm 2/7
6/16/24, 11:02 AM Escape Sequence in C
Hello.
Good morning.
My name is Ravi
The tab character (\t) represents the Tab key on the keyboard. When the tab
character is encountered in a string, it causes the cursor to move to the next
horizontal tab stop. Horizontal tab stops are usually set at intervals of eight
characters.
Example
#include <stdio.h>
int main(){
printf("Name:\tRavi\tMarks:\t50");
}
Output
Example
https://www.tutorialspoint.com/cprogramming/c_escape_sequences.htm 3/7
6/16/24, 11:02 AM Escape Sequence in C
#include <stdio.h>
int main(){
Output
These characters have a special meaning in C since " and ' symbols are used for the
representation of a character literal and a string literal respectively. Hence, to treat
these characters as a part of the string, they must be escaped with an additional
backslash preceding them.
Example
#include <stdio.h>
int main(){
printf("Welcome to \"TutorialsPoint\"\n");
printf ("\'Welcome\' to TutorialsPoint");
}
Output
Welcome to "TutorialsPoint"
'Welcome' to TutorialsPoint
https://www.tutorialspoint.com/cprogramming/c_escape_sequences.htm 4/7
6/16/24, 11:02 AM Escape Sequence in C
The escape sequence "\b", represents the backspace character. It is used erase a
character or a specific portion of a text that has already been printed on the screen.
Example
#include <stdio.h>
int main(){
Output
Welcome t TutorialsPoint
C also has a \r escape sequence. The newline escape sequence (\n) moves the
cursor to the beginning of the next line, while the carriage return escape sequence
(\r) moves the cursor to the beginning of the current line.
This escape sequence is used for Octal numbers of one to three digits. An octal
escape sequence is a backslash followed by one, two, or three octal digits (0-7). It
matches a character in the target sequence with the value specified by those digits.
Example
#include <stdio.h>
int main(){
printf("%c", '\141');
https://www.tutorialspoint.com/cprogramming/c_escape_sequences.htm 5/7
6/16/24, 11:02 AM Escape Sequence in C
return 0;
}
Output
When you run this code, it will produce the following output −
Example
#include <stdio.h>
int main(){
printf("%c", '\x41');
return 0;
}
Output
The escape sequence \a represents the alert or bell character. When executed, it
produces a sound or visual alert depending on the terminal or console being used.
Example
https://www.tutorialspoint.com/cprogramming/c_escape_sequences.htm 6/7
6/16/24, 11:02 AM Escape Sequence in C
#include <stdio.h>
int main(){
printf("Hello \a world\n");
return 0;
}
Output
Hello world
Escape sequences are used widely in many other programming languages such as
Java, PHP, C#, etc.
https://www.tutorialspoint.com/cprogramming/c_escape_sequences.htm 7/7