How to use escape characters in the C language?
In the C language, escape characters are used to represent special characters and are denoted by a backslash (\). Here are some commonly used escape characters and their usage:
- \n: indicates a newline character
Example: printf(“Hello\nWorld”); - \t is used to represent a tab space.
For example: printf(“Name\tAge”); - \r: Represents a carriage return character.
For example: printf(“Hello\rWorld”); - \b: represents the backspace character
For example: printf(“Hello\bWorld”); - \\: represents a backslash
For example: printf(“C:\Program Files”); - ‘ : indicates an apostrophe
example: printf(“It’s raining”); - Colon represents quotation marks, for example: printf(“She said, “Hello””);
- 0: Denotes a null character.
For example: char str[] = “Hello\0World”; - \xhh: Represents the character with ASCII code hh (in hexadecimal).
For example: printf(“\x48\x65\x6C\x6C\x6F”); // outputs “Hello”
The above explains how to use some commonly used escape characters, you can choose the appropriate escape character to represent special characters based on the actual situation.