1

Summary

1) The compiler will the backslash'\' at , and the characters following the backslash at 161459c8830e7a will automatically continue to the previous line

2) When connecting words, cannot have spaces after the backslash'\', and there cannot be spaces before the next line of the backslash'\'

3) continuation character'\' is suitable for define macro code blocks to improve readability

4) The escape character'\' in C language is mainly used for indicate no echo characters (which will not be displayed on the screen), and can also be used for indicate regular characters.

5) When the backslash '\' is used as an escape character, it must appear between single quotation marks or double quotation marks

\nCarriage return
\tSkip horizontally to the next tabulation position
\Backslash character'\'
\'Single quote
\dddCharacters represented by 1~3 octal numbers
\xddCharacters represented by 1~2 hexadecimal numbers
\aRing the bell
\vVertical tab
\bbackspace
\rCarriage return
\fPaper feed

Continuation character and escape character

1. The continuation character'\'

Is the following code correct?

#incl\
ud\
e <s\
tdio\
.h>

in\
t main(\
)
{
    printf(\
    "Hello World.\n"
    )\
    ;

    ret\
urn 0;
}
  • The compiler will remove the backslash'\', and the characters following the backslash will automatically be continued to the previous line

    // 将反斜杠'\'剔除,后面的字符自动接续到前一行的代码如下
    #include <stdio.h>
    
    int main()
    {
      printf(    "Hello World.\n"
      )    ;
    
      return 0;
    }
  • When connecting words, there can be no spaces after the backslash'\', and there can be no spaces before the next line of the backslash.

    // 如果格式像下面这样,urn前面有4个空格,这时候编译就不过了,因为得到的语句是
    // ret    urn 0;
      ret\
      urn 0;
  • The continuation character'\n' is suitable for use when defining macro code blocks

    // 用宏代码块实现的交换变量语句
    #define SWAP(a, b)  \
    {                   \
      int temp = a;     \
      a = b;            \
      b = temp;         \
    }

2. The escape character'\'

  • escape character'\' in C language is mainly used for indicate no echo characters (which will not be displayed on the screen), and can also be used for indicate regular characters.
  • \nCarriage return
    \tSkip horizontally to the next tabulation position
    \Backslash character'\'
    \'Single quote
    \dddCharacters represented by 1~3 octal numbers
    \xddCharacters represented by 1~2 hexadecimal numbers
    \aRing the bell
    \vVertical tab
    \bbackspace
    \rCarriage return
    fPaper feed
  • When the backslash '\' is used as an escape character, it must appear between single quotation marks or double quotation marks

    char* p = "\141 \t \'\a \x62  ";    
      // \141: 八进制141对应十进制97,Ascii中表示字符a
      // \t: 制表符
      // \': 单引号'
      // \a: 响铃一次
      // \x62: 十六进制62对应于十进制98,Ascii中表示字符b
    printf("%s\n", p);    

This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.


bryson
169 声望12 粉丝