0% found this document useful (0 votes)
33 views

Escape Sequence in C

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Escape Sequence in C

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

6/16/24, 11:02 AM 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.

Look at \n as an example. When put inside a string, the \n acts as a newline


character, generating the effect of pressing the Enter key. The following statement −

printf(" Hello \n World ");

Will produce this output −

Hello
World

The new line is an unprintable character. The \n escape sequence is useful to


generate its effect. Similarly, the escape sequence \t is equivalent to pressing the
Tab key on the keyboard.

An escape sequence is a sequence of characters that does not represent itself


when used inside a character or string literal but is translated into another character
or a sequence of characters that may be difficult or impossible to represent directly.

All Escape Sequences in C


In C, all escape sequences consist of two or more characters, the first of which is
the backslash \ (called the "Escape character"); the remaining characters have an
interpretation of the escape sequence as per the following table.

Here is a list of escape sequences available in C −

Escape sequence Meaning

\\ \ character

https://www.tutorialspoint.com/cprogramming/c_escape_sequences.htm 1/7
6/16/24, 11:02 AM Escape Sequence in C

\' ' character

\" " character

\? ? character

\a Alert or bell

\b Backspace

\f Form feed

\n Newline

\r Carriage return

\t Horizontal tab

\v Vertical tab

\ooo Octal number of one to three digits

\xhh . . . Hexadecimal number of one or more digits

Let us understand how these escape sequences work with the help of a set of
examples.

Newline Escape Sequence (\n)

The newline character, represented by the escape sequence \n in C, is used to insert


the effect of carriage return on the output screen. You would use this escape
sequence to print text in separate lines and improve the readability of output.

Example

Take a look at the following example −

#include <stdio.h>

int main(){

printf("Hello.\nGood morning.\nMy name is Ravi");


}

Output

https://www.tutorialspoint.com/cprogramming/c_escape_sequences.htm 2/7
6/16/24, 11:02 AM Escape Sequence in C

On running this code, you will get the following output −

Hello.
Good morning.
My name is Ravi

Tab Escape Sequence (\t)

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

Take a look at the following example −

#include <stdio.h>

int main(){

printf("Name:\tRavi\tMarks:\t50");
}

Output

Run the code and check its output −

Name: Ravi Marks: 50

Backslash Escape Sequence (\\)

To add backslash character itself as a part of a string, it must precede by another


backslash. First backslash escapes out of the string, and the second one takes the
effect.

Example

Take a look at the following 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(){

printf("Directory in Windows: C:\\users\\user");


}

Output

On running this code, you will get the following output −

Directory in Windows: C:\users\user

Double and Single Quotes Escape Sequences (\" and \')

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

Take a look at the following example −

#include <stdio.h>

int main(){

printf("Welcome to \"TutorialsPoint\"\n");
printf ("\'Welcome\' to TutorialsPoint");
}

Output

Run the code and check its output −

Welcome to "TutorialsPoint"
'Welcome' to TutorialsPoint

Backspace Escape Sequence (\b)

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

Check the following example code −

#include <stdio.h>

int main(){

printf("Welcome to\b TutorialsPoint");


}

Output

Run the code and check its output −

Welcome t TutorialsPoint

Note that o from to has been erased.

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.

Octal Number Escape Sequence (\ooo)

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

Take a look at the following 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 −

Hexadecimal Number Escape Sequence (\xhh)

A hexadecimal escape sequence is a backslash followed by the letter "x" followed by


two hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence
with the value specified by the two digits.

Example

Take a look at the following example −

#include <stdio.h>

int main(){

printf("%c", '\x41');

return 0;
}

Output

Here, you will get the following output −

Alert or Bell Number Escape Sequence (\a)

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

Take a look at the following 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

Run the code and check its 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

You might also like