免费注册 查看新帖 |

Chinaunix

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

[新手入门] 宏定义中的#,##操作符和... and _ _VA_ARGS_ _(zz) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-23 17:44 |只看该作者 |倒序浏览
原文地址:http://blog.chinaunix.net/u/22630/showart_285761.html
1.Preprocessor Glue: The ## Operator
预处理连接符:##操作符
Like
the # operator, the ## operator can be used in the replacement section
of a function-like macro.Additionally, it can be used in the
replacement section of an object-like macro. The ## operator combines
two tokens into a single token.
##将两个符号连接成一个。
For example, you could do this:
#define XNAME(n) x ## n
Then the macro
XNAME(4)
would expand to the following:
x4
Listing 1 uses this and another macro using ## to do a bit of token gluing.
// glue.c -- use the ## operator
#include
#define XNAME(n) x ## n
#define PRINT_XN(n) printf("x" #n " = %d\n", x ## n);
int main(void)
{
  int XNAME(1) = 14;  // becomes int x1 = 14;
  int XNAME(2) = 20;  // becomes int x2 = 20;
  PRINT_XN(1);        // becomes printf("x1 = %d\n", x1);
  PRINT_XN(2);        // becomes printf("x2 = %d\n", x2);
    return 0;
}
Here's the output:
x1 = 14
x2 = 20
Note
how the PRINT_XN() macro uses the # operator to combine strings and the
## operator to combine tokens into a new identifier.
2.Variadic Macros: ... and _ _VA_ARGS_ _
Some
functions, such as printf(), accept a variable number of arguments. The
stdvar.h header file,provides tools for creating user-defined functions
with a variable number of arguments. And C99 does the same thing for
macros.Although not used in the standard, the word variadic has come
into currency to label this facility. (However, the process that has
added stringizing and variadic to the C vocabulary has not yet led to
labeling functions or macros with a fixed number of arguments as
fixadic functions and normadic macros.)
The
idea is that the final argument in an argument list for a macro
definition can be ellipses (that is, three periods)(省略号). If so, the
predefined macro _ _VA_ARGS_ _ can be used in the substitution part to
indicate what will be substituted for the ellipses. For example,
consider this definition:
#define PR(...) printf(_ _VA_ARGS_ _)
Suppose you later invoke the macro like this:
PR("Howdy");
PR("weight = %d, shipping = $%.2f\n", wt, sp);
For the first invocation, _ _VA_ARGS_ _ expands to one argument:
"Howdy"
For the second invocation, it expands to three arguments:
"weight = %d, shipping = $%.2f\n", wt, sp
Thus, the resulting code is this:
printf("Howdy");
printf("weight = %d, shipping = $%.2f\n", wt, sp);
Listing 2 shows a slightly more ambitious example that uses string concatenation and the # operator:
// variadic.c -- variadic macros
#include
#include
#define PR(X, ...) printf("Message" #X ": " _ _VA_ARGS_ _)
int main(void)
{
    double x = 48;
    double y;
    y = sqrt(x);
    PR(1, "x = %g\n", x);
    PR(2, "x = %.2f, y = %.4f\n", x, y);
    return 0;
}
In the first macro call, X has the value 1, so #X becomes "1". That makes the expansion look like this:
(#为参数加双引号。)
print("Message " "1" ": " "x = %g\n", x);
Then the four strings are concatenated, reducing the call to this:
print("Message 1: x = %g\n", x);
Here's the output:
Message 1: x = 48
Message 2: x = 48.00, y = 6.9282
Don't forget, the ellipses have to be the last macro argument:
#define WRONG(X, ..., Y)  #X #_ _VA_ARGS_ _ #y(这个是错误的例子。)
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/23382/showart_470089.html

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
2 [报告]
发表于 2008-07-02 11:22 |只看该作者
多谢。学习了。
BTW,这样的宏:
#define LIST_FIND(head, cmpfn, type, args...)
最后一个参数是如何扩展呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP