如何在宏字符串中嵌入整数?

时间:2015-09-01 17:31:05

标签: c macros stringification

我第一次看到字符串化和令牌粘贴。我觉得它可能是这个宏的好工具:

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

应输出:

the number three: 3

1 个答案:

答案 0 :(得分:2)

试试这个

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

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