Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation using preprocessor

Tags:

c++

c

string

is it possible to concatenate strings during preprocessing?

I found this example

#define H "Hello "
#define W "World!"
#define HW H W

printf(HW); // Prints "Hello World!"

However it does not work for me - prints out "Hello" when I use gcc -std=c99

UPD This example looks like working now. However, is it a normal feature of c preprocessor?

like image 913
Vladimir Keleshev Avatar asked Sep 03 '25 14:09

Vladimir Keleshev


1 Answers

Concatenation of adjacent string litterals isn't a feature of the preprocessor, it is a feature of the core languages (both C and C++). You could write:

printf("Hello "
       " world\n");
like image 63
AProgrammer Avatar answered Sep 05 '25 05:09

AProgrammer