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;
}
这样编译就能通过了。