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

C Escape Sequences

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 <stdio.h>
int main() {
	printf("Tab:\tNewline:\n");
	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 <stdio.h>
int main() {
	printf("Name:\tAge\nAlice:\t30");
	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 <stdio.h>
int main() {
	printf("She said \"hello\"");
	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 <stdio.h>
int main() {
	printf("Path: C:\\folder");
	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 <stdio.h>
int main() {
	printf("Alert\a");
	return 0;
}
🔒

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.