Notes – Escape Sequence in C
Escape sequences are special characters used in C to represent actions like a new line, tab space, or printing special characters.
They begin with a backslash \, followed by a character that represents a specific control instruction.
These are mostly used inside character and string literals.
Why Use Escape Sequences?
- To format output.
- To control special characters that cannot be typed directly.
- To print quotes, backslashes, etc. inside strings.
Commonly Used Escape Sequences
| Escape Sequence | Meaning | Example | Output Description |
|---|---|---|---|
\n | New line | printf("Hello\nWorld"); | Prints “Hello”, then “World” on the next line |
\t | Horizontal tab | printf("Name\tAge"); | Inserts a tab between “Name” and “Age” |
\\ | Backslash | printf("C:\\Files"); | Prints a single backslash \ |
\' | Single quote | printf("It\'s done"); | Prints single quote ' |
\" | Double quote | printf("She said \"Hi\""); | Prints double quotes inside text |
\r | Carriage return (rarely used) | Moves cursor to beginning of line (used in console apps) | |
\b | Backspace | printf("A\bB"); | Removes previous character |
\a | Alert (beep sound) | printf("\a"); | Triggers system beep (if supported) |
\0 | Null character (end of string) | Used to terminate strings |
Example Code
#include <stdio.h>
int main() {
printf("Line1\nLine2\n");
printf("Tab\tSpace\n");
printf("Path: C:\\Program Files\\\n");
printf("Quote: \"Hello World\"\n");
return 0;
}
