printf(“string1”“string2”) 这是有效的 C 语言吗?
当我错误地写下这个时,我试图弄清楚一些事情
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。连续的字符串文字在 C.6.4.5 / 4 的解析早期被连接起来
:
Yes it is. Consecutive string literals are concatenated early in the parsing of C.
6.4.5 / 4:
是的,在编译时连接字符串常量非常有用。
或者
Yes, and it can be very useful to concatenate string constants at compile-time.
or
是的,它是有效的,并且长期以来一直是 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.
正如其他人所说,是的,它是有效的。我只想补充一点,输入填充多行的长字符串确实很有用。您不必乱用
\
来指示字符串继续,并且也不想添加回车符,因此您只需编写:(注意每个字符串末尾的空格,这是一个常见的错误,在这种情况下,“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:(watch out the spaces at the end of each string, it is a common mistake. In this case, "string" and "that" would be joint.)