Escape Sequences in C

C programming language is widely used for system programming and low-level applications. In order to represent special characters in C programming language, we use a set of characters called escape sequences. Escape sequences are used to represent control characters, special characters, and other characters that cannot be represented directly in the program. In this article, we will discuss the escape sequences in C programming language, their usage, and examples.



(toc)

Escape Sequences in C

Escape sequences are special character combinations used to represent non-printable characters or control codes within a string. They are preceded by a backslash (\) and allow you to include characters like newlines, tabs, and special control characters in your strings.


Common Escape Sequences:

Escape SequenceDescription
\nNewline
\tHorizontal tab
\rCarriage return
\vVertical tab
\fForm feed
\bBackspace
\aAlert (bell)
\?'Single quote
\"Double quote
\\Backslash

Examples:


printf("Hello, world!\n"); // Prints "Hello, world!" followed by a newline
printf("Tab:\tTab\n"); // Prints "Tab:\tTab\n" with a tab character between the words
printf("Single quote: \' \n"); // Prints "Single quote: ' \n" with a single quote

Using Escape Sequences in Strings

Escape sequences can be used within string literals to include special characters. For example:


char str[] = "Hello\nWorld";
printf("%s", str);

This will print:

Hello
World

Key Points:

  • Escape sequences are used to represent non-printable characters or control codes.
  • They are preceded by a backslash (\).
  • Common escape sequences include \n, \t, \r, \v, \f, \b, \a, \?, \", and \\.
  • Escape sequences can be used within string literals to include special characters.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!