c语言#、##、 ## __VA_ARGS__ 的用法

在看linux内核kernel.h里面看到这样一个语句:

#define pr_err(fmt, ...) \

eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)

看不懂尴尬,于是百度了一下,感谢度娘和博主!

转载于:http://www.cnblogs.com/alexshi/archive/2012/03/09/2388453.html

http://www.cnblogs.com/zhujudah/archive/2012/03/22/2411240.html

首先,#define 是常见的预编译命令,但是假如#放在其它地方用有些巧妙的地方。

1,先来看一个例子:

 

 

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

#define Func1(x) printf("the square of "  #x " is %d.\n",(x)*(x))

int main(void)
{
    int aa=30;
    Func1(aa);
    Func1(30);
    system("pause");
    return 0;
}

 

输出:

the square of aa is 900.

the square of 30 is 900.

 

分析》:#用在预编译语句里面可以把预编译函数的变量直接格式成字符串;注意,不能直接在其它非预编译函数直接使用#aa的形式,假如main函数里面出现printf("the square of " #x " is %d.\n",(x)*(x))是不能通过编译的.

 

2,

 

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

#define Func3(a) printf("the square of " #a " is %d.\n",b##a)
int main(void)
{
    int m=30;   
    int bm=900;
    Func3(m);         //展开后相当于 printf("the square of m is %d.\n",bm); 
    system("pause");
    return 0;
}

 输出:

the square of m is 900.

 

分析》:## 是宏连接符,作变量链接,Func(a)里面有b##a,也就是说直接连接成b‘a’,Func3(m)对应bm,由于bm在main里面有定义,所以可以打印出来。

 

3,##__VA_ARGS__这里的‘##’有特殊作用,

 

  __VA_ARGS__是可变参数宏,用法如下:

#define Debug(...) printf(__VA_ARGS__)

  使用的时候只需要:

Debug("Y = %d\n", y);

此时编译器会自动替换成printf("Y = %d\n", y");

 

 

对于##__VA_ARGS__的‘##’符号的用法,

例如:#define debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)

 

假如可变参数宏为空的时候,”“##”的作用就是让编译器忽略前面一个逗号,不然编译器会报错。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值