本文介绍了C 字符串追加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 C 字符串要附加,结果应该分配给 lhs 变量.我看到了一个静态初始化代码,如:

I've got two C strings that I want to append and result should be assigned to an lhs variable. I saw a static initialization code like:

char* out = "May God" "Bless You";.

输出真的是May GodBless You" 打印出来.我知道这个结果可能是一些未定义行为的输出.

The output was really "May GodBless You" on printing out. I understand this result can be output of some undefined behaviour.

代码实际上是在生产中,从来没有给出错误的结果.而且我们并不是只在一个地方有这样的声明.它可以在非常稳定的代码的多个地方看到,并用于形成 sql 查询.

The code was actually in production and never gave wrong results. And it was not like we had such statements only at one place. It could be seen at multiple places of very much stable code and were used to form sql queries.

C 标准是否允许这种连接?

Does C standard allow such concatenation?

推荐答案

是的,有保证.

摘自http://en.wikipedia.org/wiki/C_syntax#String_literal_concatenation :

相邻的字符串文字是在编译时连接;这个允许拆分长字符串多行,也允许字符串C 预处理器产生的文字定义和要附加到的宏编译时的字符串

这篇关于C 字符串追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 08:08