为什么C不识别多条线的字符串?
(我对 C 很陌生。)
视觉换行符在 C 中似乎并不重要。 例如:
int i; int j;
与 相同
int i;
int j;
与
int k = 0 ;
相同
int
k
=
0
;
那么为什么
"hello
hello"
与 不同
"hello hello"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为包含起始引号字符而不是结束引号字符的行比尝试跨多行写入字符串更有可能是键入错误或其他错误,因此决定字符串文字不会跨越源行,除非在行尾故意用
\
表示。此外,当发生此类错误时,编译器将面临读取可能数千行代码的情况,然后确定没有结束引号字符(已到达文件末尾)或找到其他字符串的开始引号字符。文本,然后尝试将该字符串文本的内容解析为 C 代码。除了给早期编译器带来有限的计算资源负担之外,这还可能导致远离缺少的引号字符的源代码部分出现令人困惑的错误消息。
此选择在 C 2018 6.4.5 1 中生效,它表示字符串文字为
"
s-char-sequenceopt"
,其中 s-char-sequence 是字符集的任何成员,除了引号字符、反斜杠或换行符(字符串文字也可能具有编码前缀,一个第一个"
之前的u8
、u
、U
或L
)。It is because a line that contains a starting quote character and not an ending quote character was more likely a typing mistake or other error than an attempt to write a string across multiple lines, so the decision was made that string literals would not span source lines, unless deliberately indicated with
\
at the end of a line.Further, when such an error occurs, the compile would be faced with reading possibly thousands of lines of code before determining there was no closing quote character (end of file was reached) or finding what was intended as an opening quote character for some other string literal and then attempting to parse the contents of that string literal as C code. In addition to burdening early compilers with limited compute resources, this could result in confusing error messages in a part of the source code far removed from the missing quote character.
This choice is effected in C 2018 6.4.5 1, which says that a string literal is
"
s-char-sequenceopt"
, where s-char-sequence is any member of the character set except the quote character, a backslash, or a new-line character (and a string literal may also have an encoding prefix, au8
,u
,U
, orL
before the first"
).字符串可以通过在换行符前面放置一个反斜杠来继续换行:
或者(更好)使用字符串连接:
请注意,空格已被仔细保留,因此这些等同于
"hello hello"
except用于文件中出现后的行编号。反斜杠换行符消除是在翻译过程的早期完成的 - 在概念性的第二阶段 翻译阶段。
请注意,没有删除前导空格或任何内容。如果你这样写:
那么该字符串在
in it
和and some
之间有一个(至少)8 个空格的序列。计数 8 假定printf()
语句在左边距中对齐;如果它是缩进的,则需要添加与缩进相对应的额外空格。Strings can be continued over newlines by putting a backslash immediately before the newline:
Or (better), using string concatenation:
Note that the space has been carefully preserved so that these are equivalent to
"hello hello"
except for the line numbering in the file after the appearance.The backslash-newline line elimination is done very early in the translation process — in phase 2 of the conceptual translation phases.
Note that there is no stripping leading blanks or anything. If you write:
Then the string has a sequence of (at least) 8 blanks in it between
in it
andand some
. The count of 8 assumes that theprintf()
statement is aligned in the left margin; if it is indented, you'd need to add the extra white space corresponding to the indentation.每个字符串的双引号:
的一个问题是,我们需要逃脱特殊字符,例如引号标记。
1-使用
**这种方法 需要为每行编写引号。
1- using double quotes for each string :
** One problem with this approach is that we need to escape specially characters such as quotation mark " itself.
2- Using - \ :
** This form is a lot easier to write, we don't need to write quotation marks for each line.
例如,该行
由 6 个标记组成:int、i、;、int、j 和;。大多数时候,特别是在这种情况下,空间量(空格、制表符和换行符)并不重要。这就是为什么编译器会同等对待
。
写作
就像写作一样
,希望编译器将其视为就像
关键字之间不允许有空格一样,字符串文字标记中不允许有换行符。但在需要时可以使用换行符“\n”将其包含在内。
跨行写入字符串使用字符串连接方法
虽然推荐使用上述方法,但也可以使用反斜杠
使用反斜杠方法,请注意新行中的开头空格。该字符串将包含该行中的所有内容,直到找到结束引号或另一个反斜杠。
For example, the line
consists of 6 tokens: int, i, ;, int, j and ;. Most of the time, and particularly in this case, the amount of space (space, tab and newline characters) is not critical. That's why the compiler will treat
The same.
Writing
Is like writing
and hope that the compiler treat it as
Just like space is not allowed between a keyword, newline character is not allowed in a string literal token. But it can be included using the newline escape '\n' when needed.
To write strings across lines use string concatenation method
Although the above method is recommended, you can also use a backslash
With the backslash method, beware of the beginning space in a new line. The string will include everything in that line until it finds a closing quote or another backslash.