免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2802 | 回复: 4
打印 上一主题 下一主题

#define宏的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-04 20:27 |只看该作者 |倒序浏览
请问下面的宏为什么要加上##,是变长参数的原因吗?
  1. #define DEBUG(fmt, args...) fprintf(stderr, fmt, ##args)
复制代码

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2012-01-04 23:12 |只看该作者
好象是去除多余的逗号的,免得出现fprintf( ……,)而语法错误

论坛徽章:
0
3 [报告]
发表于 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

论坛徽章:
0
4 [报告]
发表于 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)

论坛徽章:
0
5 [报告]
发表于 2012-01-05 11:14 |只看该作者
回复 4# x5miao


    没看过C99
    不过既然这么说了 应该没错
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP