← Back to C Course | Chapter 2: Input & Output | Lesson 5 of 7

C Escape Sequences

Escape sequences are special two-character codes inside text that stand for things like a new line or a tab, like secret shortcuts. The backslash tells C 'the next letter is special'.
Syntax
c
printf("first\tsecond\nnew line\\ backslash \"quote\"");

What are Escape Sequences?

An escape sequence is a two-character combination starting with a backslash that represents a character you can't type directly into a string literal -- like a line break, a tab, or the quote character the string itself uses as a delimiter.

Example: What are Escape Sequences?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Tab:\tNewline:\n");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Newline ( ) and Horizontal Tab ( )

\n moves the cursor to the beginning of the next line and is the standard way to end output lines in C, while \t inserts a tab-width space, useful for aligning columns of text in printed output.

Example: Newline ( ) and Horizontal Tab ( )

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Name:\tAge\nAlice:\t30");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing Quotes (" and \')

Because C strings are delimited by double quotes, writing a literal " inside one would end the string early and confuse the compiler -- \" tells the compiler to treat that character as literal text instead of a delimiter.

Example: Printing Quotes (" and \')

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("She said \"hello\"");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing a Backslash (\)

Since the backslash itself starts every escape sequence, printing an actual backslash character requires writing two in a row (\\) -- the first backslash escapes the second, telling the compiler you mean the character literally.

Example: Printing a Backslash (\)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Path: C:\\folder");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Audible Alert Bell (\a)

\a produces the terminal's audible alert sound (if the terminal supports it) rather than printing a visible character -- historically used to get a user's attention, though it's rarely relied on in modern interfaces.

Example: Audible Alert Bell (\a)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Alert\a");
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing a Windows path such as "C:\new\test", where \n and \t become a newline and a tab; each backslash must be written as \\.
  2. Putting a double quote inside a string without escaping it as \", which ends the string early and breaks the code.
  3. Writing /n instead of \n for a new line, which just prints a slash and the letter n.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.