并列字符串被连接成新的字符串
在分隔符“,”间的多个字符串
- 分隔符间的多个字符串属于并列关系,在翻译阶段时,多个并列的字符串会被连接到一起,形成新的字符串;
- c++标准文档引用:
String literals placed side-by-side are concatenated at translation phase 6 (after the preprocessor).
That is, "Hello," " world!" yields the (single) string "Hello, world!".
If the two strings have the same encoding prefix (or neither has one), the resulting string will have the same encoding prefix (or no prefix).
Phase 6
Adjacent string literals are concatenated.
- 范例:
[root@localhost c]# cat test_printf.c
#include <stdio.h>
static const char * const MAX[] = {
"large_" "and_" "big",
"great_" "and_" "huge"
};
int main()
{
printf("Hello %s!" "We will %s!\n", "world", "better");
printf("MAX[0] = %s\n", MAX[0]);
printf("MAX[1] = %s\n", MAX[1]);
return 0;
}
[root@localhost c]# ./test_printf.o
Hello world!We will better!
MAX[0] = large_and_big
MAX[1] = great_and_huge
[root@localhost c]#
相关链接
https://en.cppreference.com/w/cpp/language/translation_phases
https://en.cppreference.com/w/cpp/language/string_literal
注:
本文是对于c++标准文档的一个粗浅的理解,如有错漏,请各位朋友指正,谢谢