C语言中##称为连接符,其功能是在带参数的宏定义中将两个子串(token)联接起来,从而形成一个新的子串。
要注意下面的用法:
1、
[cpp]
#include <stdio.h>
#define debug(format, args...) fprintf(stderr, format, args)
void main(void){
debug("Test \n");
return;
}
有的说这种情况下字符串后面会多一个逗号,但是我用gcc编译不通过;
2、
[cpp]
#include <stdio.h>
#define debug(format, args...) fprintf(stderr, format, ##args)
//#define debug(format, args...) fprintf(stderr, format, args)
void main(void){
debug("Test \n");
return;
}
这样可以编译通过,执行正确;
3、
[cpp]
#include <stdio.h>
//#define debug(format, args...) fprintf(stderr, format, ##args)
#define debug(format, args...) fprintf(stderr, format, args)
void main(void){
debug("Test%d \n",1);
return;
}
这样也正确;
#include<stdio.h>
#include<string.h>
#define stat_name(a,b) a##b
#define debug(format,args...) fprintf(stdout,format,##args)
#define debug1(format,args) fprintf(stdout,format,args)
#define debug2(format,args...) fprintf(stdout,format,args)
int main(int argc, char** argv)
{
debug1("%s\n","hello");
debug("%s\n","hello");
debug("test define\n");
debug("%s,%d\n","this is age",10);
debug2("this is %d\n",10);
debug2("this is %s %d\n","this is",10);
return 0;
}
hello
hello
test define
this is age,10
this is 10
this is this is 10