Escape Sequences

In this tutorial, we will understand the use and importance of escape sequences in the C programming language.


Introduction

Escape sequences in C are special characters that we use to represent certain characters that we can’t directly type on a regular keyboard. These characters are either non-printable or have special meanings. We use escape sequences to format strings in specific ways or to include characters that aren’t normally allowed within a string.

To indicate that we’re using an escape sequence, we start with a backslash (\) followed by a specific character or combination of characters.

The following table shows the list of  most commonly used escape sequences in C:

Escape SequenceName
\nNewline character
\tHorizontal tab
\bBackspace
\rCarriage return
\”Double quote
\’Single quote
\\Backslash

Let’s see these characters in action.


Newline (\n)

The newline character is used to move the cursor at the beginning of the next line.
For example:

printf(“Hello!\nI am John.”);

Output:

Hello!
I am John.

Tab (\t)

Tab (\t) represents the horizontal tab character and is used to insert a horizontal tab.
For example:

printf(“Hello!\tI am John.”);

Output:

Hello!    I am John.

Backspace (\b)

Backspace (\b) is used to move the cursor back one position.
For example:

printf(“Hello!\b I am John.”);

Output:

Hello I am John.

Carriage Return (\r)

Carriage return (\r) is used to move the cursor at the beginning of the current line.
For example:

printf(“Hello!\rI am John.”);

Output:

I am John.

Double Quote (\”)

Double quote (\”) is used to include a double quote within a string as simply adding a double quote within a string is not allowed.

For example:

printf(“I love the \”C programming\” language.”);

Output:

I love the “C programming” language.

Single Quote (\’)

Single quote (\’) is used to include a single quote within a string as simply adding a single quote within a string is not allowed.

For example:

printf(“I love \’C\’.”);

Output:

I love ‘C’.

Backslash (\\)

Backslash (\\) is used to include a backslash within a string.
For example:

printf(“Path: C:\\Programs”);

Output:

Path: C:\Programs

Other Escape Sequences

Following are some extra escape sequences which are good to know:

Alarm or Beep (\a)

The \a character is used to generate a beep sound.
For example, the following program when executed will generate a beep sound after printing the output.

int main()
{
    printf("I love C\a.");
    return 0;
}

Question Mark (\?)

The question mark (\v?) character is used to display a question mark symbol within a string.
For example:

printf("What is 2 + 2\?");

Output:

What is 2 + 2?

Octal Number (\0oo)

The octal number escape sequence (\0oo) allows you to specify a character using its octal value. The ‘oo’ represents one to three octal digits (0-7), which correspond to the character’s ASCII code.

For example:

printf("The character with octal value \\051 is ‘%c’", ‘\051’);

Output:

The character with octal value \051 is ')'

We are getting the above output because the octal value ‘\051’ represents the ASCII code for the character ‘)’. This can be checked in the following ASCII table:

The decimal equivalent of the octal number 51 is 41. Therefore, character ‘)’ lies in row 41.

Hexadecimal Number (\xhh)

The hexadecimal number escape sequence (\xhh) allows you to specify a character using its hexadecimal value. The ‘hh’ represents one to two hexadecimal digits (0-9, A-F or a-f), which correspond to the character’s ASCII code.

For example:

printf("The character with hexadecimal value \\x41 is '%c'", '\x41');

Output:

The character with hexadecimal value \x41 is 'A'

NULL (\0)

The NULL escape sequence (\0) is used to represent the null character. It has a value of 0 and is often used to terminate strings in C.

For example:

printf("Hello\0World!");

Output:

Hello

In the output, the string is terminated after the character ‘o’ because \0 represents the null character and indicates the end of the string. So, no character will be displayed after the null character.


Summary

This tutorial has provided an overview of escape sequences in the C programming language and their importance. Escape sequences are special characters used to represent non-printable characters or characters that can’t be directly typed on a keyboard. They are used for formatting strings and including special characters within them.

We have learned some commonly used escape sequences like:

  1. Newline (\n): Moves the cursor to the beginning of the next line.
  2. Tab (\t): Inserts a horizontal tab.
  3. Backspace (\b): Moves the cursor back one position.
  4. Carriage Return (\r): Moves the cursor to the beginning of the current line.
  5. Double Quote (\”): Allows inclusion of a double quote within a string.
  6. Single Quote (\’): Allows inclusion of a single quote within a string.
  7. Backslash (\): Allows inclusion of a backslash within a string.

and we have learned some not so common escape sequences as well:

  1. Alarm or Beep (\a): Generates a beep sound.
  2. Question Mark (\?): Displays a question mark symbol within a string.
  3. Octal Number (\0oo): Specifies a character using its octal value.
  4. Hexadecimal Number (\xhh): Specifies a character using its hexadecimal value.
  5. NULL (\0): Represents the null character, commonly used to terminate strings.

Overall, the tutorial serves as a handy reference for understanding and working with escape sequences in C.



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.