[英]C: Concatenating __COUNTER__ with a string
I want to stringify the __COUNTER__
and print it as a string not an integer, is that possible? 我想将
__COUNTER__
字符串化并打印为字符串而不是整数,可以吗?
printf("%s\n", #__COUNTER__);
I have tried the following but it does not work 我尝试了以下方法,但是它不起作用
hello.c:6:19: error: stray ‘#’ in program
printf("%s\n",#__COUNTER__);
Yes, but it takes an indirection because you can't use the #
operator outside of a macro. 是的,但是需要一个间接指令,因为您不能在宏外部使用
#
运算符。
#define STRINGIFY_2(a) #a
#define STRINGIFY(a) STRINGIFY_2(a)
printf("%s\n", STRINGIFY(__COUNTER__));
The double macro is required to expand __COUNTER__
, otherwise the result would be "__COUNTER__"
. 需要double宏来扩展
__COUNTER__
,否则结果将为"__COUNTER__"
。
If you don't want to reinvent that wheel, that's exactly what BOOST_PP_STRINGIZE
does. 如果您不想重新发明轮子,那正是
BOOST_PP_STRINGIZE
所做的。
You have to stringify it with a preprocessor macro. 您必须使用预处理器宏对其进行字符串化。
#define XSTR(s) STR(s)
#define STR(s) #s
printf("%s", XSTR(counter));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.