Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed an integer in a macro string? [duplicate]

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
like image 210
tarabyte Avatar asked Sep 06 '25 19:09

tarabyte


1 Answers

try this

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

#define MY_NUMBER   3
#define MY_STRING   "the number three: " S(MY_NUMBER)
like image 54
BLUEPIXY Avatar answered Sep 10 '25 05:09

BLUEPIXY