4

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
tarabyte
  • 17,837
  • 15
  • 76
  • 117
  • 1
    What's wrong with `printf("%s %d\n", MY_STRING, MY_NUMBER)`??? – barak manos Sep 01 '15 at 17:33
  • The printf is just for testing. The real question is meant to be creating the macro. – tarabyte Sep 01 '15 at 17:39
  • Preprocessor operators (# and ##) do not work inside quoted string literals. You cannot use token concatenation to concatenate string literals, either; only individual tokens. (However, you can concatenate two string literals by just writing them one after another: `"Hello, " "world!"`). And finally, you can only use the stringify operator (#) on macro parameters. @alk: The linked answer is good but I fear that it is not really an answer to this confusion. – rici Sep 01 '15 at 17:48

1 Answers1

2

try this

#define S_(x) #x
#define S(x) S_(x)

#define MY_NUMBER   3
#define MY_STRING   "the number three: " S(MY_NUMBER)
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70