C Escape Sequences
Some characters in C cannot be typed or printed directly — like a newline, a tab, or a backslash. Escape sequences solve this problem. An escape sequence is a combination of a backslash \ and a character that together represent a special character or action.
Escape sequences are used inside string literals and character constants. The compiler recognizes these two-character combinations and replaces them with the intended special character or action.
Why Escape Sequences Exist
Consider these situations:
- You want to move the cursor to a new line — but pressing Enter in the code just creates a new line in the source file, not in the output.
- You want to print a double-quote character — but double quotes already mark the start and end of a string.
- You want to print a backslash — but a backslash is the escape character itself.
Escape sequences handle all of these situations cleanly.
Complete List of Escape Sequences in C
| Escape Sequence | Name | What It Does |
|---|---|---|
\n | Newline | Moves the cursor to the next line |
\t | Horizontal Tab | Inserts a tab space (usually 8 spaces) |
\r | Carriage Return | Moves the cursor to the beginning of the same line |
\\ | Backslash | Prints a literal backslash character |
\' | Single Quote | Prints a literal single quote |
\" | Double Quote | Prints a literal double quote inside a string |
\a | Alert (Bell) | Produces a beep sound (system-dependent) |
\b | Backspace | Moves cursor one position back |
\f | Form Feed | Moves cursor to next page (used in printing) |
\v | Vertical Tab | Moves cursor down by one tab stop vertically |
\0 | Null Character | Represents the end of a string (ASCII 0) |
\xhh | Hexadecimal Value | Character with hex value hh |
\ooo | Octal Value | Character with octal value ooo |
The Most Common Escape Sequences
\n — Newline
This is the most frequently used escape sequence. It ends the current line and starts a new one.
#include <stdio.h>
int main()
{
printf("Line 1\nLine 2\nLine 3\n");
return 0;
}
Output:
Line 1
Line 2
Line 3
\t — Horizontal Tab
Use \t to align output in columns, like a table.
#include <stdio.h>
int main()
{
printf("Name\tAge\tCity\n");
printf("Alice\t25\tDelhi\n");
printf("Bob\t30\tMumbai\n");
return 0;
}
Output:
Name Age City
Alice 25 Delhi
Bob 30 Mumbai
\\ — Backslash
To print a single backslash, you must write two backslashes in the code.
#include <stdio.h>
int main()
{
printf("C:\\Users\\Alice\\Documents\n");
return 0;
}
Output:
C:\Users\Alice\Documents
\" — Double Quote Inside String
#include <stdio.h>
int main()
{
printf("She said, \"Hello!\"\n");
return 0;
}
Output:
She said, "Hello!"
\' — Single Quote Inside Character Literal
#include <stdio.h>
int main()
{
char apostrophe = '\'';
printf("It's a single quote: %c\n", apostrophe);
return 0;
}
\0 — Null Character (String Terminator)
Every string in C ends with \0 automatically. It marks where the string stops.
#include <stdio.h>
int main()
{
char name[] = {'A', 'l', 'i', 'c', 'e', '\0'};
printf("%s\n", name); // Output: Alice
return 0;
}
Without the \0, the program would not know where the string ends and would read garbage memory.
\r — Carriage Return
The carriage return moves the cursor back to the start of the current line — it does not create a new line. This is most visible in terminal environments.
#include <stdio.h>
int main()
{
printf("Hello, World!\rHi");
printf("\n");
return 0;
}
Output (overwrites "Hello" with "Hi"):
Hi llo, World!
Hex and Octal Escape Sequences
You can specify any character using its ASCII value in hexadecimal or octal format.
#include <stdio.h>
int main()
{
printf("\x41\n"); // Hex 41 = decimal 65 = 'A'
printf("\101\n"); // Octal 101 = decimal 65 = 'A'
printf("\x48\x69\n"); // H i — outputs: Hi
return 0;
}
Output:
A
A
Hi
Escape Sequences in Character Constants
Escape sequences also work inside single-quoted character constants.
char newline = '\n'; // newline character
char tab = '\t'; // tab character
char null_char = '\0'; // null character
Visual Diagram: How Escape Sequences Work
Source code string: "Hello\nWorld"
^
|
Compiler sees \n and converts it
to newline character (ASCII 10)
|
v
Output on screen:
Hello
World
- The two characters \ and n become ONE special character (newline)
- This happens at compile time, not at runtime
Common Mistakes with Escape Sequences
| Mistake | Wrong Code | Correct Code |
|---|---|---|
| Printing a backslash | printf("C:\name"); | printf("C:\\name"); |
| Printing a quote | printf("He said "Hi""); | printf("He said \"Hi\""); |
Using \n outside quotes | printf(\n); | printf("\n"); |
Summary
Escape sequences in C let you represent special characters and actions inside strings and character constants. A backslash \ followed by a specific character creates the sequence. The most commonly used are \n for newline, \t for tab, \\ for a literal backslash, and \" for a double quote inside a string. The null character \0 terminates every C string. Hex and octal escape sequences allow you to represent any ASCII character by its numeric value. Mastering escape sequences gives you precise control over how your program displays text output.
