问题描述
假设在C,我有以下的code:
Suppose in C, I have the following code:
i=5 \
+6;
如果我打印 I
,它给了我11。
If I print i
, it gives me 11.
我不明白上面的code正确执行。乍一看,我猜这是因为无法识别的标记 \\
的编译器错误。有人能解释什么逻辑?是它最大适合的逻辑关系?
I do not understand how the above code executes correctly. At first glance, I guessed it to be compiler error because of unrecognized token \
. Can somebody explain the logic? Is it related to maximal munch logic?
推荐答案
在一行的末尾的反斜线告诉编译器忽略新行字符。
A backslash at the end of a line tells the compiler to ignore the new-line character.
有格式化线是可读的对人类不中断的源文本的一种方法。例如,如果你有引号括起来的长字符串,你可以用一个反斜杠,继续在新行字符串不插入字符串中的换行符。
It is a way of formatting lines to be readable for humans without interrupting the source text. E.g., if you have a long string enclosed in quotation marks, you can use a backslash to continue the string on a new line without inserting a new-line character in the string.
(这是比较有用之前C标准补充说,邻近的字符串,例如ABC,高清
,被连接的属性。现在你可以将字符串连续行,他们将被连接起来。在这之前,你必须使用反斜杠做到这一点。)
(This was more useful before the C standard added the property that adjacent strings, such as "abc" "def"
, are concatenated. Now you can put strings on consecutive lines, and they will be concatenated. Prior to that, you had to use the backslash to do it.)
目前反斜杠最常见的用途是作为指出,要继续preprocessor宏定义。不同于常规的C语句,preprocessor语句必须在同一行。然而,一些preprocessor宏定义是相当长的。要格式化(有点)很好,一个定义是在多个物理线S $ P $垫,但反斜线使得他们在同一行编译器(包括preprocessor)。
Nowadays the most common use of the backslash is, as heretolearn points out, to continue preprocessor macro definitions. Unlike regular C statements, preprocessor statements must be on a single line. However, some preprocessor macro definitions are quite long. To format them (somewhat) nicely, a definition is spread over multiple physical lines, but the backslash makes them into one line for the compiler (including the preprocessor).
一个反斜杠后跟一个换行符是的完全由编译器的源文本删除,不像本身就是一个换行符。所以原文:
A backslash followed by a new-line character are completely removed from the source text by the compiler, unlike a new-line character by itself. So the source text:
abc\
def
等同于单一的标识符 ABCDEF
,而不是 ABC DEF
。你可以在不同的字母序列(三字符序列,如 ?? =
任何操作员或其他语言结构的中间使用,转换为替换字符,如#
,反斜线全新的在线处理前):
is equivalent to the single identifier abcdef
, not abc def
. You can use it in the middle of any operator or other language construction except trigraph sequences (trigraph sequences, such as ??=
, are converted to replacement characters, such as #
, before the backslash-new-line processing):
MyStructureVariable-\
>MemberName
IncrementMe+\
+
不过,不这样做。合理地使用它。
However, do not do that. Use it reasonably.
这篇关于评估声明中部前pression用反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!