C/C++ macro string concatenation

tvr picture tvr · Mar 10, 2011 · Viewed 199.6k times · Source
#define STR1      "s"
#define STR2      "1"
#define STR3      STR1 ## STR2

Is it possible to concatenate have STR3 == "s1"? You can do this by passing args to another Macro function. But is there a direct way?

Answer

Sean picture Sean · Mar 10, 2011

If they're both strings you can just do:

#define STR3 STR1 STR2

This then expands to:

#define STR3 "s" "1"

and in the C language, separating two strings with space as in "s" "1" is exactly equivalent to having a single string "s1".