printf(“string1”“string2”) 这是有效的 C 语言吗?

发布于 2024-10-07 00:25:35 字数 238 浏览 5 评论 0原文

当我错误地写下这个时,我试图弄清楚一些事情

printf("string1""string2");

令我惊讶的是它编译并产生了一个串联的字符串输出,即

string1string2

这是有效的C吗?

我正在使用 gcc 版本 4.4.1 (Ubuntu 4.4.1-4ubuntu9)

I was trying to figure out something when I wrote this by a mistake

printf("string1""string2");

To my surprise it compiled and produced a concatenated string output i.e

string1string2

Is this valid C?

I am using gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9)

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

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

发布评论

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

评论(4

以酷 2024-10-14 00:25:35

是的。连续的字符串文字在 C.6.4.5 / 4 的解析早期被连接起来

在转换阶段 6 中,由任何相邻字符序列和宽字符串文字标记指定的多字节字符序列将连接成单个多字节字符序列。如果任何标记是宽字符串文字标记,则生成的多字节字符序列将被视为宽字符串文字;否则,它被视为字符串文字。

Yes it is. Consecutive string literals are concatenated early in the parsing of C.

6.4.5 / 4:

In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal; otherwise, it is treated as a character string literal.

长梦不多时 2024-10-14 00:25:35

是的,在编译时连接字符串常量非常有用。

#define VERSION "1.0"
#define COMPANY "Trivial Software"

printf("hello world: v. " VERSION " copyright (c) " COMPANY);

或者

puts(
  "blah blah blah\n"
  "blah blah blah\n"
  "blah blah blah\n"
  "blah blah blah\n"
);

Yes, and it can be very useful to concatenate string constants at compile-time.

#define VERSION "1.0"
#define COMPANY "Trivial Software"

printf("hello world: v. " VERSION " copyright (c) " COMPANY);

or

puts(
  "blah blah blah\n"
  "blah blah blah\n"
  "blah blah blah\n"
  "blah blah blah\n"
);
扶醉桌前 2024-10-14 00:25:35

是的,它是有效的,并且长期以来一直是 C 语言的一部分(如果不是从一开始)。连接是在编译时完成的。

Yes, it is valid and has been part of the C language for a very long time (if not since the beginning). The concatenation is done at compile time.

一江春梦 2024-10-14 00:25:35

正如其他人所说,是的,它是有效的。我只想补充一点,输入填充多行的长字符串确实很有用。您不必乱用 \ 来指示字符串继续,并且也不想添加回车符,因此您只需编写:(

"very long string "
"that continues over here"

注意每个字符串末尾的空格,这是一个常见的错误,在这种情况下,“string”和“that”是连在一起的。)

As other said, yes, it is valid. I only wanted to add that it is really useful to input long strings that fill several lines. You don't have to mess with \ to indicate the string continues, and don't wanting to add a carriage return too, so you just write:

"very long string "
"that continues over here"

(watch out the spaces at the end of each string, it is a common mistake. In this case, "string" and "that" would be joint.)

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