Javatpoint Logo
Javatpoint Logo

Escape Sequence in C

Programming languages like C have escape sequences as a standard feature. They enable the inclusion of special characters and control patterns in strings and character constants that are difficult to represent or write directly. In this blog post, we will explore escape sequences in C, their syntax, usage, and examples with matching outputs. Therefore, let's start this adventure to understanding escape sequences in C.

An escape sequence in the C programming language consists of a backslash () and a character that stands in for a special character or control sequence. During the compilation process, the C compiler substitutes any escape sequences it comes across with the relevant character or control sequence. It enables the use of difficult-to-represent characters like newlines, tabs, quotations, and backslashes.

It is composed of two or more characters starting with backslash \. For example: \n represents new line.

Regular Escape Sequences:

There are several escape sequence in C programming languages.

Escape SequenceMeaning
\aAlarm or Beep
\bBackspace
\fForm Feed
\nNew Line
\rCarriage Return
\tTab (Horizontal)
\vVertical Tab
\\Backslash
\'Single Quote
\"Double Quote
\?Question Mark
\nnnoctal number
\xhhhexadecimal number
\0Null

Now that we have a thorough understanding of each escape sequence in C,

Alarm or Beep (\a):

The alarm or beep escape sequence (a) produces an audible alert or beep sound.

Output:

This is an alarm sound:

Backspace (\b):

The cursor can be advanced by one character with the backspace escape key (b).

Output:

HelloWorld!

Form Feed (\f):

The form feed escape sequence (f) is used to mimic a page break or advance to the next page.

Output:

This is before the form feed.
                            This is after the form feed.

New Line (\n):

The new line escape sequence (n) is used to insert a newline character and move the cursor to the start of the following line.

Output:

Line 1
Line 2

Carriage Return (\r):

The cursor can be moved to the start of the current line by using the carriage return escape sequence (r).

Output:

World!

Tab (Horizontal) (\t):

The tab escape sequence (t) is used to insert a horizontal tab character and shift the cursor to the following tab stop.

Output:

Name:   John    Age:    25

Vertical Tab (\v):

The vertical tab escape sequence (v) is used to simulate a vertical tab or shift the mouse to the following vertical tab location.

Output:

Hello
     World!

Backslash (\):

A backslash character is inserted using the backslash escape sequence (\).

Output:

This is a backslash: \Hello

Single Quote ('):

The single quote escape sequence (') is used to insert a single quote character.

Output:

This is a single quote: 'Hello'

Double Quote ("):

A double quotation character is inserted using the double quote escape sequence (").

Output:

This is a double quote: "Hello"

Question Mark (?):

The question mark escape sequence (?) is used to insert a question mark character.

Output:

This is a question mark: ?

Octal Number (\nnn):

The character's octal value can be inserted using the octal number escape sequence (nnn).

Output:

This is an octal value: A

Hexadecimal Number (\xhh):

A character's hexadecimal value can be inserted using the hexadecimal number escape sequence (xhh).

Output:

This is a hexadecimal value: A

Null (\0):

The null character, denoted by "0", is inserted using the null escape sequence (0).

Output:

String: Hello

Conclusion:

In conclusion, escape sequences are a crucial component of C programming that enables us to successfully handle special characters and control sequences. We may put characters into strings and character variables that are challenging to directly express using escape sequences.


Next TopicASCII value in C



Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA