Search code examples
cmacrosstringification

How to embed an integer in a macro string?


My first time seeing stringification and token pasting. I feel like it could be a good tool for this macro:

#define MY_NUMBER   3
#define MY_STRING   "the number three: ##MY_NUMBER"
printf("%s\n", MY_STRING);

should output:

the number three: 3

Solution

  • try this

    #define S_(x) #x
    #define S(x) S_(x)
    
    #define MY_NUMBER   3
    #define MY_STRING   "the number three: " S(MY_NUMBER)