Error when defining a stringising macro with __VA_ARGS__

删除回忆录丶 提交于 2019-12-04 02:17:59

This happens unless there's at least one variable argument. You can try this GNU extension to fix it:

#define DBG(format, ...) printf("DEBUG: " #format "\n", ##__VA_ARGS__)
                                                        ^^

As explained in the GNU doc:

[if] the variable argument is left out when the macro is used, then the comma before the ‘##’ will be deleted.

Check out this on MSDN. It contains info on Variadic Macros, which is what you are using.

Why do you need to stringise format, it can stay as is, just treat it as a string when using the macro.

The error, as cnicutar propsed can be solved with adding '##' before the VA_ARGS

#define DBG(format, ...) printf("DEBUG: " format "\n", ##__VA_ARGS__)

Usage example:

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