免费注册 查看新帖 |

Chinaunix

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

[C] 可变参数... args...有什么区别? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-04-25 10:44 |只看该作者 |倒序浏览
可变参数宏定义的时候,可以用 ... 格式 和 args... 格式。
... 使用时用 __VA_ARGS__   或  ##__VA_ARGS__
args... 使用时 用 args 或者 ##args


这两个方式有什么区别?



#define trace_printk(fmt, ...)                \
do {                            \
    char _______STR[] = __stringify((__VA_ARGS__));    \
    if (sizeof(_______STR) > 3)            \
        do_trace_printk(fmt, ##__VA_ARGS__);    \
    else                        \
        trace_puts(fmt);            \
} while (0)

#define do_trace_printk(fmt, args...)                    \
do {                                    \
    static const char *trace_printk_fmt                \
        __attribute__((section("__trace_printk_fmt"))) =    \
        __builtin_constant_p(fmt) ? fmt : NULL;            \
                                    \
    __trace_printk_check_format(fmt, ##args);            \
                                    \
    if (__builtin_constant_p(fmt))                    \
        __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);    \
    else                                \
        __trace_printk(_THIS_IP_, fmt, ##args);            \
} while (0)


论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
2 [报告]
发表于 2017-04-25 13:29 |只看该作者
__VA_ARGS__ 是以前的写法,
##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
3 [报告]
发表于 2017-04-26 08:24 |只看该作者
__VA_ARGS__ 和 ##__VA_ARGS__ 的区别
Note: some compilers offer an extension that allows ## to appear after a comma and before __VA_ARGS__, in which case the ## does nothing when __VA_ARGS__ is non-empty, but removes the comma when __VA_ARGS__ is empty: this makes it possible to define macros such as fprintf (stderr, format, ##__VA_ARGS__)

论坛徽章:
0
4 [报告]
发表于 2017-04-26 12:23 |只看该作者
folklore 发表于 2017-04-25 13:29
__VA_ARGS__ 是以前的写法,
##args是新写法吧~

恩,我试了下编译出来汇编都一样
就是格式不同
Args...这种格式更容易阅读
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP