Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C string append

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";.

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

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.

Does C standard allow such concatenation?

like image 259
Mayank Avatar asked Sep 14 '26 22:09

Mayank


2 Answers

Yes, it is guaranteed.

Extract from http://en.wikipedia.org/wiki/C_syntax#String_literal_concatenation :

Adjacent string literals are concatenated at compile time; this allows long strings to be split over multiple lines, and also allows string literals resulting from C preprocessor defines and macros to be appended to strings at compile time

like image 140
Simone Avatar answered Sep 16 '26 16:09

Simone


Yes. this concatenation is allowed in C, it is not undefined behavior.

Although I think it should produce "May GodBless You" (since there is no space in the quoted part)

like image 40
MByD Avatar answered Sep 16 '26 16:09

MByD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!