#, ##,...,__VA_ARGS__,##__VA_ARGS__

1、ANSI C允许在c字符串中包含宏参数,如下例子:

#include <stdio.h>
#include <stdlib.h>
#define POSI(x)  printf("the square of "#x     "is : %d\n", (x) * (x))
int main()
{
        int y = 4;
        POSI(4);
        POSI(2+4);
        return 0;

}

通过gcc -E 展开宏如下:
int main()
{
 int y = 4;
 printf("the square of ""4" "is this : %d\n", (4) * (4));
 printf("the square of ""2+4" "is this : %d\n", (2+4) * (2+4));
 return 0;

}

由此可以看出,printf格式字符串中包含的#x 在预处理中展开,并忽略后面所有空格。

2、##

#include <stdio.h>
#include <stdlib.h>
#define POSI(x)  printf("the square of "#x     "is this : %d\n", (x) * (x))
#define NAME(y)  b##y
#define ARRY(x) printf("the arry" #x "%d", b##x)

int main()
{
        int y = 4;
        POSI(4);
        POSI(2+4);
        ARRY(4);
        return 0;

}

通过gcc -E 展开宏如下:

int main()
{
 int y = 4;
 printf("the square of ""4" "is this : %d\n", (4) * (4));
 printf("the square of ""2+4" "is this : %d\n", (2+4) * (2+4));
 printf("the arry" "4" "%d", b4);
 return 0;

}

##是字符连接器

3、可变宏 ...和_ _VA_ARGS_ _

#include <stdio.h>
#include <stdlib.h>


#define POSI(x)  printf("the square of "#x     "is this : %d\n", (x) * (x))
#define NAME(y)  b##y


#define ARRY(x) printf("the arry" #x "%d", b##x)


#define PR(...)  printf(__VA_ARGS__) 


int main()
{
        int y = 4;
        POSI(4);
        POSI(2+4);
        ARRY(4);
        PR("hello.\n");
        return 0;

}

gcc -E 编译:

int main()
{
 int y = 4;
 printf("the square of ""4" "is this : %d\n", (4) * (4));
 printf("the square of ""2+4" "is this : %d\n", (2+4) * (2+4));
 printf("the arry" "4" "%d", b4);
 printf("hello.\n");
 return 0;

}

 ...和_ _VA_ARGS_ _实现了可变参宏。但要注意...只能替代最后面的宏参数,例如:

#define PR1(x,...,y) 

这样定义是错误的。

4、##__VA_ARGS__ 

经常我们会遇到如下形式的宏定义:

#define pr_notice(fmt, ...) \

printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)

在这里##有特殊的意义,那就是...为空的时候忽略前面的逗号,否则编译会报错。

#include <stdio.h>
#include <stdlib.h>


#define POSI(x)  printf("the square of "#x     "is this : %d\n", (x) * (x))
#define NAME(y)  b##y


#define ARRY(x) printf("the arry" #x "%d", b##x)


#define PR(...)  printf(__VA_ARGS__) 


#define PR1(fmt, ...) printf(fmt, __VA_ARGS__)
int main()
{
        int y = 4;
        int b4;
        POSI(4);
        POSI(2+4);
        ARRY(4);
        PR1("hello .\n");
        return 0;
}

gcc -E 展开后:

int main()
{
 int y = 4;
 int b4;
 printf("the square of ""4" "is this : %d\n", (4) * (4));
 printf("the square of ""2+4" "is this : %d\n", (2+4) * (2+4));
 printf("the arry" "4" "%d", b4);
 printf("hello .\n", );
 return 0;

}

看到了吗,这样编译器无法通过的。

加了##:

#include <stdio.h>
#include <stdlib.h>


#define POSI(x)  printf("the square of "#x     "is this : %d\n", (x) * (x))
#define NAME(y)  b##y


#define ARRY(x) printf("the arry" #x "%d", b##x)


#define PR(...)  printf(__VA_ARGS__) 


#define PR1(fmt, ...) printf(fmt, ##__VA_ARGS__)
int main()
{
        int y = 4;
        int b4;
        POSI(4);
        POSI(2+4);
        ARRY(4);
        PR1("hello .\n");
        return 0;

}

gcc -E 编译后:

int main()
{
 int y = 4;
 int b4;
 printf("the square of ""4" "is this : %d\n", (4) * (4));
 printf("the square of ""2+4" "is this : %d\n", (2+4) * (2+4));
 printf("the arry" "4" "%d", b4);
 printf("hello .\n");
 return 0;

}

这样编译就能通过了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值