gpt4 book ai didi

c - 两个字符串文字的串联如何工作?

转载 作者:太空狗 更新时间:2023-10-29 16:24:44 28 4
gpt4 key购买 nike

char* a="dsa" "qwe";
printf("%s", a);

输出:dsaqwe

我的问题是为什么这东西会起作用。如果我在两个字符串文字之间留一个空格或什么都不填,它会连接字符串文字。

这是如何运作的?

最佳答案

它由 ISO C 标准定义,相邻的字符串文字合并为一个。

语言有点枯燥(毕竟是标准)但是 C116.4.5 String literals 部分指出:

In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and identically-prefixed wide string literal tokens are concatenated into a single multibyte character sequence.

同一标准的 5.1.1.2 Translation phases, point 6 中也提到了这一点,但更加简洁:

Adjacent string literal tokens are concatenated.

这基本上意味着 "abc""def""abcdef" 没有什么不同。

它通常对制作长字符串很有用,同时仍具有良好的格式,例如:

const char *myString = "This is a really long "
"string and I don't want "
"to make my lines in the "
"editor too long, because "
"I'm basically anal retentive :-)";

关于c - 两个字符串文字的串联如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12120944/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com