C Escape Sequences

Some characters like a new line or a tab cannot be typed inside a string directly. For these we use escape sequences. An escape sequence starts with a backslash \ followed by a letter.

Common escape sequences

SequenceMeaning
\nNew line
\tHorizontal tab
\\Backslash
\"Double quote
\'Single quote
\rCarriage return (cursor to start of the line)
\bBackspace
\aAlert (beep sound)
\0Null character (end of a string)

Example

#include <stdio.h>

int main(void) {
    printf("Name:\tRobin\n");
    printf("City:\tDelhi\n");
    printf("He said, \"Learn C\"\n");
    printf("Path: C:\\Users\\Robin\n");
    printf("Line one\nLine two\n");
    return 0;
}

Output:

Output of the above program is

Name: Robin
City: Delhi
He said, "Learn C"
Path: C:\Users\Robin
Line one
Line two

Note: If you want to print a percent sign with printf(), write %%. It is not an escape sequence, but it is a similar need.

Next: C Format Specifiers.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.