Skip to content
StudyFame

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 \0 is.

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 sequenceNameWhat it does
\nNew lineMoves the cursor to the next line
\tHorizontal tabMoves to the next tab stop (adds space)
\\BackslashPrints one \
\"Double quotePrints " inside a string
\'Single quotePrints ' (needed in a character constant)
\0Null characterMarks the end of a string
\aAlert (bell)Makes a beep sound (if supported)
\bBackspaceMoves the cursor one place back
\rCarriage returnMoves the cursor to the start of the same line
\?Question markPrints ? (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

  1. \t moves the cursor to the next tab stop (usually every 8 columns), so the marks line up in a column.
  2. \n at the end of each line moves to the next line.
  3. 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 an n. 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 \0 at 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

MistakeWhat happensFix
Using /n instead of \nPrints "/n" as textUse the backslash \
printf("He said "Hi"");Compile error: the string ends earlyprintf("He said \"Hi\"");
printf("C:\new");\n becomes a new lineprintf("C:\\new");
Forgetting \n at the endThe next output joins the same lineEnd lines with \n

Escape Sequences Practice Questions

  1. What is an escape sequence? Give two examples.
  2. How do you print a backslash in C?
  3. What is the difference between \n and \r?
  4. What is \0 used for?
  5. Write a printf statement to print: She said "Yes"
Show answers
  1. A backslash followed by a character that represents a special character. Examples: \n, \t.
  2. Write \\.
  3. \n moves to the next line; \r moves to the start of the same line.
  4. It marks the end of a string (null character).
  5. 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)

  1. Which escape sequence moves to a new line?

    A) \t   B) \n   C) \b   D) \a

  2. How do you print a double quote inside a string?

    A) ""   B) \"   C) /"   D) '"'

  3. Which escape sequence marks the end of a string?

    A) \n   B) \e   C) \0   D) \s

  4. How many characters is '\n'?

    A) 1   B) 2   C) 3   D) 0

  5. Which escape sequence makes a beep?

    A) \b   B) \a   C) \r   D) \v

Show answers
  1. B: \n
  2. B: \"
  3. C: \0
  4. A: 1 character.
  5. B: \a (alert).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Starts withBackslash \
Counts asOne character
\n / \tNew line / Tab
\\ / \" / \'Backslash / Double quote / Single quote
\0Null: end of string
\a / \b / \rBeep / 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?
\n goes to the next line; \r goes 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.