C Programming Tutorial

C Programming Tutorial

Escape Sequence in C Programming

Hello Everyone, In this article, we will cover the concept of Escape Sequence in C Programming. Escape sequences are special characters used to perform specific tasks like creating new lines, tabs, or displaying certain characters. Whether you’re new to C or have some experience, this guide will help you to understand Escape Sequence in C effectively. Let’s get started.

In the C programming language, an escape sequence is a combination of characters that represents a special character or control sequence. An escape sequence begins with a backslash () character, which indicates that the following characters should be treated as a single special character, rather than as ordinary text.

Escape sequences are commonly used to represent characters that are difficult or impossible to type directly, such as tabs, newlines, and quotes. For example, the escape sequence “\t” represents a horizontal tab character, while “\n” represents a newline character. The escape sequence “”” represents a double quote character, which is useful when you need to include a double quote character in a string literal.

Table of all the common escape sequences in C:

Escape sequenceMeaning
\nnewline
\thorizontal tab
\rcarriage return
\bbackspace
\fform feed
\aalert (bell)
\backslash
single quote
double quote
\0null character (ASCII code 0)
\xhhcharacter with ASCII code hh (hexadecimal notation)
\ooocharacter with ASCII code ooo (octal notation)

The escape sequence “\0” represents the null character, which is used to terminate strings in C. The escape sequences “\xhh” and “\ooo” can be used to represent any character with an ASCII code between 0 and 255.

Here’s an example C program that demonstrates the use of escape sequences:

#include <stdio.h>int main() {
   printf("Hello\\tworld!\\n");
   printf("This is a backslash: \\\\ \\n");
   printf("She said, \\"Hello!\\" \\n");
   return 0;
}

In this program, we are using three different escape sequences:

  • \\t : represents a horizontal tab
  • \\n : represents a newline
  • \\" : represents a double quote

The printf() function is used to print out the messages to the console. The first printf() statement prints out “Hello” followed by a horizontal tab and then “world!” on the same line. The \\t escape sequence is used to insert the horizontal tab character.

The second printf() statement prints out “This is a backslash: \”. Since a backslash is a special character in C, we need to use the escape sequence \\\\ to print out a single backslash character.

The third printf() statement prints out “She said, “Hello!”” with the double quotes included. Since double quotes are also a special character in C, we need to use the escape sequence \\" to print out a double quote character.

When we run this program, the output will be:

Hello   world!
This is a backslash: \\
She said, "Hello!"
Share This Article