C Escape Sequences
In this page:
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 <stdio.h>
int main() {
printf("Tab:\tNewline:\n");
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 <stdio.h>
int main() {
printf("Name:\tAge\nAlice:\t30");
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 <stdio.h>
int main() {
printf("She said \"hello\"");
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 <stdio.h>
int main() {
printf("Path: C:\\folder");
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 <stdio.h>
int main() {
printf("Alert\a");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: