Escape Sequences in C
How do you print a new line, a tab, or a double quote inside a string? You cannot just type them, because they have special meaning in C.
Instead, you use a backslash \ followed by a letter, like \n. These are called escape sequences. In this lesson you will learn all of them with examples.
What You Will Learn
- What an escape sequence is.
- The full list of escape sequences in C.
- How to print quotes and backslashes.
- How to make neat columns with
\t. - What the null character
\0is.
Escape Sequences in C Explained
Exam definition: An escape sequence is a combination of a backslash (\) and a character, used inside a character or string constant to represent a special character such as a new line or a tab.
| Escape sequence | Name | What it does |
|---|---|---|
\n | New line | Moves the cursor to the next line |
\t | Horizontal tab | Moves to the next tab stop (adds space) |
\\ | Backslash | Prints one \ |
\" | Double quote | Prints " inside a string |
\' | Single quote | Prints ' (needed in a character constant) |
\0 | Null character | Marks the end of a string |
\a | Alert (bell) | Makes a beep sound (if supported) |
\b | Backspace | Moves the cursor one place back |
\r | Carriage return | Moves the cursor to the start of the same line |
\? | Question mark | Prints ? (rarely needed) |
Memory trick: The letter is the first letter of the job: n = new line, t = tab, a = alert, b = backspace, r = return. To print a special symbol itself (\ " '), put a backslash before it.
Why "escape"?
The backslash tells the compiler: "the next character escapes its normal meaning". For example, " normally ends a string, but \" is just a quote mark to print.
Syntax of an Escape Sequence
printf("Line 1\nLine 2"); /* inside a string */
char newline = '\n'; /* as a character constant */
An escape sequence is written as two characters, but it counts as one character.
Example 1: Make a Table with \t and \n
#include <stdio.h>
int main(void)
{
printf("Name\tMarks\n");
printf("Riya\t90\n");
printf("Aman\t85\n");
return 0;
}
Output
Name Marks Riya 90 Aman 85
Code Explanation
\tmoves the cursor to the next tab stop (usually every 8 columns), so the marks line up in a column.\nat the end of each line moves to the next line.- Without
\n, all three lines would print on one line.
How Escape Sequences Work
- When the compiler reads
"\n", it does not store a backslash and ann. It stores one special character: the newline (ASCII 10). - When that character reaches the screen, the terminal moves to the next line instead of printing a symbol.
- That is why
sizeof("\n")is 2: one byte for the newline and one for the hidden\0at the end of every string.
Example 2: Print Quotes and a Windows File Path
#include <stdio.h>
int main(void)
{
printf("Teacher said, \"C is easy!\"\n");
printf("It\'s my first program.\n");
printf("Saved in: C:\\Users\\Riya\\hello.c\n");
return 0;
}
Output
Teacher said, "C is easy!" It's my first program. Saved in: C:\Users\Riya\hello.c
Inside a string, a single quote can be typed directly (It's also works). The \' is required only inside a character constant: char q = '\'';
Important Points to Remember
- Every escape sequence starts with a backslash
\. - It counts as one character.
\n= new line,\t= tab.- To print
\write\\; to print"write\". \0(null) marks the end of a string.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Using /n instead of \n | Prints "/n" as text | Use the backslash \ |
printf("He said "Hi""); | Compile error: the string ends early | printf("He said \"Hi\""); |
printf("C:\new"); | \n becomes a new line | printf("C:\\new"); |
Forgetting \n at the end | The next output joins the same line | End lines with \n |
Escape Sequences Practice Questions
- What is an escape sequence? Give two examples.
- How do you print a backslash in C?
- What is the difference between
\nand\r? - What is
\0used for? - Write a printf statement to print:
She said "Yes"
Show answers
- A backslash followed by a character that represents a special character. Examples:
\n,\t. - Write
\\. \nmoves to the next line;\rmoves to the start of the same line.- It marks the end of a string (null character).
printf("She said \"Yes\"\n");
Coding Exercise
Print this multiplication table header and two rows using \t for columns:
n n*2 n*3 1 2 3 2 4 6
Show solution
#include <stdio.h>
int main(void)
{
printf("n\tn*2\tn*3\n");
printf("1\t2\t3\n");
printf("2\t4\t6\n");
return 0;
}
Quick Quiz: Escape Sequences in C (MCQ)
-
Which escape sequence moves to a new line?
A)
\tB)\nC)\bD)\a -
How do you print a double quote inside a string?
A)
""B)\"C)/"D)'"' -
Which escape sequence marks the end of a string?
A)
\nB)\eC)\0D)\s -
How many characters is
'\n'?A) 1 B) 2 C) 3 D) 0
-
Which escape sequence makes a beep?
A)
\bB)\aC)\rD)\v
Show answers
- B:
\n - B:
\" - C:
\0 - A: 1 character.
- B:
\a(alert).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Starts with | Backslash \ |
|---|---|
| Counts as | One character |
\n / \t | New line / Tab |
\\ / \" / \' | Backslash / Double quote / Single quote |
\0 | Null: end of string |
\a / \b / \r | Beep / Backspace / Start of line |
Frequently asked questions
- Why is it called an escape sequence?
- The backslash makes the next character escape its normal meaning.
- What is the null character?
'\0', with value 0. It marks the end of every string in C.- What is the difference between \n and \r?
\ngoes to the next line;\rgoes back to the start of the same line.- How do you print a file path like C:\new?
- Use a double backslash:
"C:\\new".
Remember
- Letter = job: new line, tab, alert, backspace, return.
- To print a special symbol, put
\before it.
Next Topic
Next lesson: Type Conversion and Type Casting in C. You will learn how C converts one data type into another, automatically and by force.