为什么C不识别多条线的字符串?

发布于 2025-01-20 12:12:16 字数 333 浏览 6 评论 0 原文

(我对 C 很陌生。)

视觉换行符在 C 中似乎并不重要。 例如:

int i; int j;

与 相同

int i;
int j;

int k = 0 ;

相同

int
k
=
0
;

那么为什么

"hello
hello"

与 不同

"hello hello"

(I am very new to C.)

Visual newlines seem to be unimportant in C.
For instance:

int i; int j;

is same as

int i;
int j;

and

int k = 0 ;

is same as

int
k
=
0
;

so why is

"hello
hello"

not the same as

"hello hello"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

她如夕阳 2025-01-27 12:12:16

这是因为包含起始引号字符而不是结束引号字符的行比尝试跨多行写入字符串更有可能是键入错误或其他错误,因此决定字符串文字不会跨越源行,除非在行尾故意用 \ 表示。

此外,当发生此类错误时,编译器将面临读取可能数千行代码的情况,然后确定没有结束引号字符(已到达文件末尾)或找到其他字符串的开始引号字符。文本,然后尝试将该字符串文本的内容解析为 C 代码。除了给早期编译器带来有限的计算资源负担之外,这还可能导致远离缺少的引号字符的源代码部分出现令人困惑的错误消息。

此选择在 C 2018 6.4.5 1 中生效,它表示字符串文字为 " s-char-sequenceopt ",其中 s-char-sequence 是字符集的任何成员,除了引号字符、反斜杠或换行符(字符串文字也可能具有编码前缀,一个第一个 " 之前的 u8uUL)。

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, a u8, u, U, or L before the first ").

你的往事 2025-01-27 12:12:16

字符串可以通过在换行符前面放置一个反斜杠来继续换行:

"hello \
hello"

或者(更好)使用字符串连接:

"hello "
"hello"

请注意,空格已被仔细保留,因此这些等同于 "hello hello" except用于文件中出现后的行编号。

反斜杠换行符消除是在翻译过程的早期完成的 - 在概念性的第二阶段 翻译阶段

请注意,没有删除前导空格或任何内容。如果你这样写:

printf("Some long string with maybe an integer %d in it\
        and some more data on the next line\n", i);

那么该字符串在 in itand some 之间有一个(至少)8 个空格的序列。计数 8 假定 printf() 语句在左边距中对齐;如果它是缩进的,则需要添加与缩进相对应的额外空格。

Strings can be continued over newlines by putting a backslash immediately before the newline:

"hello \
hello"

Or (better), using string concatenation:

"hello "
"hello"

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:

printf("Some long string with maybe an integer %d in it\
        and some more data on the next line\n", i);

Then the string has a sequence of (at least) 8 blanks in it between in it and and some. The count of 8 assumes that the printf() statement is aligned in the left margin; if it is indented, you'd need to add the extra white space corresponding to the indentation.

甜是你 2025-01-27 12:12:16

每个字符串的双引号:

char *str = "hello "
"hello" ;

的一个问题是,我们需要逃脱特殊字符,例如引号标记。

1-使用

char *str = "hello \
hello" ;

**这种方法 需要为每行编写引号。

1- using double quotes for each string :

char *str = "hello "
"hello" ;

** One problem with this approach is that we need to escape specially characters such as quotation mark " itself.

2- Using - \ :

char *str = "hello \
hello" ;

** This form is a lot easier to write, we don't need to write quotation marks for each line.

少钕鈤記 2025-01-27 12:12:16

我们可以将 C 程序视为一系列标记:在不改变其含义的情况下无法拆分的字符组。标识符和关键字是标记。 + 和 - 等运算符、逗号等标点符号也是如此
和分号以及字符串文字。

例如,该行

int i; int j;

由 6 个标记组成:inti;intj 。大多数时候,特别是在这种情况下,空间量(空格、制表符和换行符)并不重要。这就是为什么编译器会同等对待

int           i
;int
j;

写作

"Hello
 Hello"

就像写作一样

un signed

,希望编译器将其视为就像

unsigned

关键字之间不允许有空格一样,字符串文字标记中不允许有换行符。但在需要时可以使用换行符“\n”将其包含在内。

跨行写入字符串使用字符串连接方法

"Hello"
"Hello"

虽然推荐使用上述方法,但也可以使用反斜杠

"Hello \
 Hello"

使用反斜杠方法,请注意新行中的开头空格。该字符串将包含该行中的所有内容,直到找到结束引号或另一个反斜杠。

We can think of a C program as a series of tokens: groups of characters that can't be split up without changing their meaning. Identifiers and keywords are tokens. So are operators like + and -, punctuation marks such as the comma
and semicolon, and string literals.

For example, the line

int i; int j;

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

int           i
;int
j;

The same.

Writing

"Hello
 Hello"

Is like writing

un signed

and hope that the compiler treat it as

unsigned

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

"Hello"
"Hello"

Although the above method is recommended, you can also use a backslash

"Hello \
 Hello"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文