转义序列'\'的用途是什么?

时间:2021-12-15 20:15:50

In the below code snippet , how is '\' behaving ?

在下面的代码片段中,'\'表现如何?

printf("hii\"); // This line gives error :  missing terminating " characterprintf("hii\  n"); // This line prints hii n 

I am unable to get how this escape sequence is behaving here ,Please explain .

我无法知道这个转义序列在这里的表现如何,请解释一下。

1 个解决方案

#1


0  

An escape sequence isn't the single \ character; it's that followed by another character. For example, \" is an escape sequence, as is \n. Under some circumstances you can see more than a single character following the backslash all as the same escape code; this has to do with how the characters are represented internally (ASCII or Unicode value) and can be safely ignored for now.

转义序列不是单个\字符;接下来是另一个角色。例如,\“是一个转义序列,因为\ n。在某些情况下,您可以在反斜杠后面看到多个单个字符作为相同的转义码;这与字符在内部表示的方式有关(ASCII或Unicode值),现在可以安全地忽略。

An escape sequence is used to write a character that is inconvenient/impossible to put into the code directly. For example, \" is the escape sequence for a quotation mark. It is like putting a quote inside the string, which you couldn't otherwise do because it would instead close the string literal. Look at the syntax highlighting of your question to see what I mean; most of the first line is considered part of the string, because you never have an unescaped closing quote.

转义序列用于写入不方便/不可能直接放入代码的字符。例如,\“是引号的转义序列。它就像在字符串中放入一个引号,否则你不能这样做,因为它会关闭字符串文字。看看你的问题的语法高亮,看看我的意思;第一行的大部分都被认为是字符串的一部分,因为你永远不会有未转义的结束语。

The most common escape sequence is perhaps \n. Unlike with \", it doesn't just produce a literal n in the string; you could do that without an escape. Instead it produces a newline. The code

最常见的转义序列可能是\ n。与\“不同,它不仅仅在字符串中生成一个文字n;你可以在没有转义的情况下做到这一点。相反,它会生成一个换行符。

printf("hii\nthere");

prints

版画

hithere

to the screen.

到屏幕。

The second line of code in your question uses the escape sequence \ (backslash space). Thisis not a standard escape sequence; if you compile with warnings your compiler will probably report that it's ignoring it or something.

您问题中的第二行代码使用转义序列\(反斜杠空格)。这不是标准的逃逸序列;如果您使用警告进行编译,您的编译器可能会报告它忽略它或其他东西。

(If you want to actually print a backslash to the screen, you need to escape a backslash, using \\)

(如果要在屏幕上实际打印反斜杠,则需要使用\\转义反斜杠)

#1


0  

An escape sequence isn't the single \ character; it's that followed by another character. For example, \" is an escape sequence, as is \n. Under some circumstances you can see more than a single character following the backslash all as the same escape code; this has to do with how the characters are represented internally (ASCII or Unicode value) and can be safely ignored for now.

转义序列不是单个\字符;接下来是另一个角色。例如,\“是一个转义序列,因为\ n。在某些情况下,您可以在反斜杠后面看到多个单个字符作为相同的转义码;这与字符在内部表示的方式有关(ASCII或Unicode值),现在可以安全地忽略。

An escape sequence is used to write a character that is inconvenient/impossible to put into the code directly. For example, \" is the escape sequence for a quotation mark. It is like putting a quote inside the string, which you couldn't otherwise do because it would instead close the string literal. Look at the syntax highlighting of your question to see what I mean; most of the first line is considered part of the string, because you never have an unescaped closing quote.

转义序列用于写入不方便/不可能直接放入代码的字符。例如,\“是引号的转义序列。它就像在字符串中放入一个引号,否则你不能这样做,因为它会关闭字符串文字。看看你的问题的语法高亮,看看我的意思;第一行的大部分都被认为是字符串的一部分,因为你永远不会有未转义的结束语。

The most common escape sequence is perhaps \n. Unlike with \", it doesn't just produce a literal n in the string; you could do that without an escape. Instead it produces a newline. The code

最常见的转义序列可能是\ n。与\“不同,它不仅仅在字符串中生成一个文字n;你可以在没有转义的情况下做到这一点。相反,它会生成一个换行符。

printf("hii\nthere");

prints

版画

hithere

to the screen.

到屏幕。

The second line of code in your question uses the escape sequence \ (backslash space). Thisis not a standard escape sequence; if you compile with warnings your compiler will probably report that it's ignoring it or something.

您问题中的第二行代码使用转义序列\(反斜杠空格)。这不是标准的逃逸序列;如果您使用警告进行编译,您的编译器可能会报告它忽略它或其他东西。

(If you want to actually print a backslash to the screen, you need to escape a backslash, using \\)

(如果要在屏幕上实际打印反斜杠,则需要使用\\转义反斜杠)