#: 经常在宏定义里面会使用到,主要是用来把参数转换成字符串。
#include
#define PRINT(x) printf("%s = %d\r\n", #x, x)
int main(void)
{
int a = 10, b = 11;
PRINT(a);
PRINT(b);
PRINT(a + b);
return 0;
}
#include
#define SQUARE(x) printf("The square of "#x" is %d.\n", ((x)*(x)));
int main(void)
{
int a = 6;
SQUARE(a);
return 0;
}
##:是一种运算符,是将两个运算对象连接在一起,只能出现在带参宏定义的替换文本中。
NOTE:
在单一的宏定义中,最多可以出现一次“#”或“##”预处理操作符。如果没有指定与“#”或“##”预处理操作符相关的计算次序,则会产生问题。为避免该问题,在单一的宏定义中只能使用其中一种操作符(即,一份“#”或一个“##”,或都不用)。除非非常有必要,否则尽量不要使用“#”和“##”。
#include
#define CMD_NUM(n) (cmd ## n)
int main(void)
{
int cmd1 = 11, cmd2 = 12;
printf("cmd1 = %d\r\n", CMD_NUM(1)); // CMD_NUM(1) == cmd1
printf("cmd2 = %d\r\n", CMD_NUM(2)); // CMD_NUM(2) == cmd2
return 0;
}
__VA_ARGS__
的作用是:可变参数宏(variadic macros)。用于宏定义中参数列表的最后一个参数为省略号,一般多用在调试信息。
#define DEBUG1(format, ...) printf(format, __VA_ARGS__)
//该宏定义GCC下报错,MSVC下未报错
#define DEBUG2(format, ...) printf(format, ## __VA_ARGS__)
//该宏定义GCC、MSVC下均未报错
#include
#define DEBUG1(format, ...) printf(format, __VA_ARGS__) //该宏定义GCC下报错,MSVC下未报错
#define DEBUG2(format, ...) printf(format, ## __VA_ARGS__) //该宏定义GCC、MSVC下均未报错
int main(void)
{
int a = 11;
DEBUG1("[DBG1]Hello World!!!\r\n");
DEBUG2("[DBG2]Hello World!!!\r\n");
DEBUG1("[DBG1]a = %d\r\n", a);
DEBUG2("[DBG1]a = %d\r\n", a);
return 0;
}
##__VA_ARGS__
宏前面加上##
的作用在于,当可变参数的个数为0时,这里的##
起到把前面多余的","去掉的作用,否则会编译出错。
在GCC的环境下,直接运行上面的Demo会报错:
修改后可以正常运行:
#include
#define DEBUG1(format, ...) printf(format, __VA_ARGS__) //该宏定义GCC下报错,MSVC下未报错
#define DEBUG2(format, ...) printf(format, ## __VA_ARGS__) //该宏定义GCC、MSVC下均未报错
int main(void)
{
int a = 11;
//DEBUG1("[DBG1]Hello World!!!\r\n");
DEBUG2("[DBG2]Hello World!!!\r\n");
DEBUG1("[DBG1]a = %d\r\n", a);
DEBUG2("[DBG1]a = %d\r\n", a);
return 0;
}