在ANSI C中,相邻的字符串常量将被自动合并成一个字符串。除了最后一个字符串外,其余每个字符串末尾的''字符会被自动删除(编译器的行为)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char* s1 = "hello""hello"; char* s2 = "hello" "hello"; char* s3 = "hello" "hello" "hello"; printf("%sn", s1); printf("%sn", s2); printf("%sn", s3); printf("hello" "hello"); system("PAUSE"); return 0; }
编译器忽略程序中的空格,在此基础上,可以理解为,编译器将忽略两个相邻的引号
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char* s1[] = {"hello", "hello", "hello", "hello" "hello", }; system("PAUSE"); return 0; }
上述程序,原本要创建5个hello的数组,但是漏写了一个逗号,结果编译器自动合并相邻的字符串,从而导致错误。
另外,最后一个逗号不会有问题,不管它的存在是不是有意义,ANSI C接受这样的现象。
最后
以上就是耍酷睫毛膏最近收集整理的关于相邻的字符串常量自动合并的全部内容,更多相关相邻内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复