c++cc-preprocessor

C/C++ macro string concatenation


#define STR1      "s"
#define STR2      "1"
#define STR3      STR1 ## STR2

Is it possible to concatenate STR1 and STR2, to "s1"? You can do this by passing args to another Macro function. But is there a direct way?


Solution

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