Is it possible to stringify a C macro that contains a comma?

ⅰ亾dé卋堺 提交于 2020-02-05 05:18:24

问题


Say I have this:

#define CAKE     , something

and the result I want is ", something". Can it be done?

The following doesn't work in gcc:

#define MAKE_STRING(x)  #x
#define STRING(x)       MAKE_STRING(x)

STRING(CAKE)

The compiler thinks I'm passing two arguments into MAKE_STRING() and balks.


回答1:


If your preprocessor supports variadic macros, __VA_ARGS__ will do the trick:

#define CAKE     , something

#define MAKE_STRING(...)  #__VA_ARGS__
#define STRING(x)       MAKE_STRING(x)

#include <stdio.h>
int main()
{
    printf("%s\n", STRING(CAKE) );
}



回答2:


#define CAKE     (, something)

please have a try.



来源:https://stackoverflow.com/questions/22551191/is-it-possible-to-stringify-a-c-macro-that-contains-a-comma

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!