C Escape Sequences
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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 ( )
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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 \')
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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 (\)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Writing a Windows path such as
"C:\new\test", where\nand\tbecome a newline and a tab; each backslash must be written as\\. - Putting a double quote inside a string without escaping it as
\", which ends the string early and breaks the code. - Writing
/ninstead of\nfor 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: