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:

  1. \n: indicates a newline character
    Example: printf(“Hello\nWorld”);
  2. \t is used to represent a tab space.
    For example: printf(“Name\tAge”);
  3. \r: Represents a carriage return character.
    For example: printf(“Hello\rWorld”);
  4. \b: represents the backspace character
    For example: printf(“Hello\bWorld”);
  5. \\: represents a backslash
    For example: printf(“C:\Program Files”);
  6. ‘ : indicates an apostrophe
    example: printf(“It’s raining”);
  7. Colon represents quotation marks, for example: printf(“She said, “Hello””);
  8. 0: Denotes a null character.
    For example: char str[] = “Hello\0World”;
  9. \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.

广告
Closing in 10 seconds
bannerAds