Stringizing Operator
# 使参数被双引号包住
#@ 使参数被单引号包住
## 粘结
1.转义宏#
在宏体中允许#号加于参数之前,在宏扩展时将#后的参数转义为字符串
#define PRINTF(x) printf(#x" = %d\n",x)
PRINTF(salary);
经过gcc预编器扩展为:
printf("salary"" = %d \n",salary);
即 printf("salary = %d \n",salary);
2.合并宏##
在宏体中##将两边的单词合并为一个单词。合并符两边允许有空格。
预处理器在处理时,将这些空格和##符一并删去,使合并符两边的单词合二为一
#define printconv(x) printf("token"#x" = %d",token##x)
printfconv(5);
经过gcc预编译器扩展为:
printf("token5 = %d",token5);
3.调试宏
打印出代码所在的文件、函数、行号,以及编译时间、日期等等
printf("%s %d %s %s %s\n",__FILE__, __LINE__, __FUNCTION__,__TIME__,__DATE__);
4.变参宏
#if 1
#define PRINTF_DEBUG(...)
#else
#define PRINTF_DEBUG(...) printf(__VA_ARGS__)
#endif
5.判断宏
#include
.......省略
#if(RHEL_RELEASE_CODE == RHEL_RELEASE_VERSION(4,7))
extern int hack_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt);
#elif(RHEL_RELEASE_CODE == RHEL_RELEASE_VERSION(5,4))
extern int hack_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *ori_dev);
#else
#error "This module only supports Redhat 4.7 and Redhat 5.4 only."
#endif
5.数据结构对齐宏#pragma pack(push) //保存对齐状态#pragma pack(1) //设定为紧密排列
struct test{
char m1;
double m4;
int m3;
};#pragma pack(pop) //恢复对齐状态
阅读(5210) | 评论(2) | 转发(3) |