宏函数中#, ##及参数的简单应用

来源:互联网 发布:java 打印堆栈信息 编辑:程序博客网 时间:2024/06/05 10:36
#include <stdio.h>//#的作用:下面代码相当于声明一个字符串STR[],s就是STR[]中的字符 #define STR(s) # s//##作用:将a, b 合成一个新的变量c(假设为c, c = ab). Note:c必须有声明或为常数,如 STR1(1, 2) = 12#define STR1(a, b) a ## b//...作用:...表示可变参数,而后面__VA_ARGS__相当与函数中printf()中的%d. //Note:在__VA_ARGS_前加#,则传入实参时不用"",否则加上""#define showlist(...) printf(#__VA_ARGS__)#define report(test, ...) ((test)?printf(#test):printf(__VA_ARGS__))int main(void){int alanwelcome = 3;int x = 3;int y = 4;printf(STR(hello  world\n));printf("%d\n",STR1(alan, welcome));showlist(The first, second, third items);report(x > y, "the bigger number is %d", y);return 0;}

初学预处理,做个笔记。。。