Contents
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