Programmer Coding

Escape Sequence in C Language?

Escape Sequence in C?

In C, an escape is a special sequence of characters that begins with a backslash \ followed by one or more characters. This system is used to represent characters that are difficult or difficult to type directly into code, such as newlines, tabs, or special characters. Escapes are defined by the compiler or runtime environment and are converted to the corresponding characters as the function executes.

Regular Escape Sequences:

 

Here are some commonly used escape sequences in C language

 

  • \n: Newline – Moves the cursor to the beginning of the next line.
  • \t: Horizontal Tab – Moves the cursor to the next tab stop.
  • \’: Single Quote – Represents a single quote character.
  • \”: Double Quote – Represents a double quote character.
  • \\: Backslash – Represents a backslash character.
  • \0: Null Character – Represents the null terminator character.
  • \b: Backspace – Moves the cursor one position to the left.
  • \r: Carriage Return – Moves the cursor to the beginning of the current line.
Example

#include <stdio.h>

int main() {

// Using escape sequences in a string

printf(“programmer\ncoding.com\n”);   // Newline

printf(“Name:\tRamkrishna\n”);     // Horizontal Tab

printf(“He said, \’Hi!\’\n”); // Single Quote

printf(“She said, \”Hello!\”\n”); // Double Quote

printf(“C:\\Documents\n”);    // Backslash

printf(“Hello\bWorld\n”);     // Backspace

printf(“12345\rABCD\n”);      // Carriage Return

 

return 0;

}

Output

programmer

coding.com

Name:       Ramkrishna

He said, ‘Hi!’

She said, “Hello!”

C:\Documents

HellWorld

ABCD45

Leave a Comment

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

Scroll to Top