Chinaunix

标题: #define宏的使用 [打印本页]

作者: dengjin_cu    时间: 2012-01-04 20:27
标题: #define宏的使用
请问下面的宏为什么要加上##,是变长参数的原因吗?
  1. #define DEBUG(fmt, args...) fprintf(stderr, fmt, ##args)
复制代码

作者: bruceteen    时间: 2012-01-04 23:12
好象是去除多余的逗号的,免得出现fprintf( ……,)而语法错误
作者: hbmhalley    时间: 2012-01-04 23:32
Second, the `##' token paste operator has a special meaning when placed between a comma and a variable argument. If you write

     #define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)
and the variable argument is left out when the eprintf macro is used, then the comma before the `##' will be deleted. This does not happen if you pass an empty argument, nor does it happen if the token preceding `##' is anything other than a comma.

万一 __VA_ARGS__ 为空,省去之前的逗号
http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html#Variadic-Macros
作者: x5miao    时间: 2012-01-05 01:17
回复 3# hbmhalley


    http://gcc.gnu.org/onlinedocs/cp ... tml#Variadic-Macros

有逗号的是C99的用法,没有逗号的是gcc的扩展对吗?
#define DEBUG(args,...) fprintf(stderr,args, ##__VA_ARGS__)
#define DEBUG(args...) fprintf(stderr, ##args)
作者: hbmhalley    时间: 2012-01-05 11:14
回复 4# x5miao


    没看过C99
    不过既然这么说了 应该没错




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2