Escape sequences are special characters in C that are used to represent certain non-printable or special characters in a string or character constant. An escape sequence is a combination of a backslash () followed by a character or sequence of characters.
The following are some common escape sequences in C:
- \n – Newline: This escape sequence is used to move the cursor to the beginning of the next line.
- \t – Horizontal Tab: This escape sequence is used to insert a horizontal tab.
- \r – Carriage Return: This escape sequence is used to move the cursor to the beginning of the current line.
- \b – Backspace: This escape sequence is used to move the cursor back one space.
- \f – Form Feed: This escape sequence is used to insert a form feed character.
- ‘ – Single Quote: This escape sequence is used to insert a single quote character.
- ” – Double Quote: This escape sequence is used to insert a double quote character.
- \0 – Null: This escape sequence is used to insert a null character.
C Escape Sequence Table:-
Some escape sequences are given below.
Escape Sequence | Meaning |
\n | Sends the cursor to the next line. |
\t | Horizontal Tab |
\a | Alert( Used to generate beep) |
\c | Carriage return(sends the cursor to the beginning of the current line) |
\b | backspace(sends the cursor one character to the left) |
\’ | prints the single quotation mark |
\’’ | prints the double quotation mark |
\\ | prints the backslash character |
\? | prints the question mark |
For example, if you want to print the string “Hello World” with a newline character at the end, you can use the escape sequence \n as follows:
printf("Hello World\n");
Or, if you want to print the string “She said, “Hello!”” with double quotes, you can use the escape sequence ” as follows:
printf("She said, \"Hello!\"\n");
In conclusion, escape sequences are a useful feature in C that allow programmers to insert non-printable or special characters into strings and character constants. By using escape sequences, C programs can handle a wider range of characters and produce more readable and well-formatted output.