两个字符串文字的串联是如何工作的? [英] How does concatenation of two string literals work?

查看:35
本文介绍了两个字符串文字的串联是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

输出:dsaqwe

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

My question is why does this thing work. If I give a space or nothing in between two string literals it concatenates the string literals.

这是如何工作的?

推荐答案

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

It's defined by the ISO C standard, adjacent string literals are combined into a single one.

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

The language is a little dry (it is a standard after all) but section 6.4.5 String literals of C11 states:

在翻译阶段 6 中,由任何相邻字符序列和相同前缀的宽字符串文字标记指定的多字节字符序列连接成单个多字节字符序列.

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 phase, point 6 中也有提到,不过更简洁一些:

This is also mentioned in 5.1.1.2 Translation phases, point 6 of the same standard, though a little more succinctly:

相邻的字符串文字标记被连接起来.

Adjacent string literal tokens are concatenated.

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

This basically means that "abc" "def" is no different to "abcdef".

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

It's often useful for making long strings while still having nice formatting, something like:

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 :-)";

这篇关于两个字符串文字的串联是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆