现在的位置: 首页 > 综合 > 正文

gcc中的#和##操作符以及调试宏

2013年12月04日 ⁄ 综合 ⁄ 共 797字 ⁄ 字号 评论关闭

gcc中的#和##操作符以及调试宏

 

from

http://hi.baidu.com/deep_pro/blog/item/0c297a1e1aa75d1540341746.html

 

gcc在标准c上作了很多扩展,而Linux内核代码也依赖gcc的一些特性,这样限制了使用其他编译器编译内核
gcc本身又支持多种cpu构架,我觉得这样也是Linux的自我保护

#是字符串化操作符,可以把当前内容转换为字符串
##是连接操作符,可以在预处理阶段实现字符串的链接操作

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

#define dprintf(expr) printf("%s=%d/n",#expr,expr)
#define test(x) test ## x
void test1()
{
printf("this is %s/n",__FUNCTION__);
}
void test2()
{
printf("this is %s /n",__FUNCTION__);
}

int main(int argc, char *argv[])
{
dprintf(1+1);
test(1)();
test(2)();
return EXIT_SUCCESS;
}

根据gcc的这些特性,通常可见两种调试宏
#define DEBUG_OUT (fmt,args...) /
{/
printf("File:%s Function:%s Line:%d",__FILE__,__FUNUCTION__,__LINE__);/
printf(fmt,##args);/
}

#define DEBUG_OUT (fmt,args...) /
printf("FILE:%s Function:%s Line:%d "fmt,__FILE__,__FUNCTION__,__LINE__,##args);

这样的调试宏的缺点是fmt只能是字符串常量,不能像printf一样使用字符串指针了

抱歉!评论已关闭.