#运算符用于在预编译时,将宏参数转换为字符串
eg.
#include <stdio.h>
#define CONVERT(f)(#f)
void helloworld()
{
printf("hi,tom welcome to you!");
}
int main()
{
printf("%s
",CONVERT(hello world!));
printf("%s
",CONVERT(100));
printf("%s
",CONVERT(while));
printf("%s
",CONVERT(return));
printf("%s
",CONVERT(helloworld));
return 0;
}
输出:
printf("%s ",CONVERT(helloworld)); 这句并没有执行函数里面的内容,没有输出:printf("hi,tom welcome to you!");
CONVERT宏只是输出了函数的名称。
##运算符则用于在预编译时,粘连两个符号
一般情况下,我们是这么定义结构体的:
#include <stdio.h>
typedef struct _student
{
int id;
char* name;
}student;
int main()
{
student s1;
s1.id=1;
s1.name="tth";
printf("id:%d
",s1.id);
printf("name:%s
",s1.name);
student *p1;
p1=&s1;
printf("p1-id:%d
",p1->id);
printf("p1-name:%s
",p1->name);
return 0;
}
当有了##运算符符后,我们可以这么定义结构体:
#include <stdio.h>
#define STUDENT(type)typedef struct _##type type;
struct _##type
STUDENT(student)
{
int id;
char *name;
};
int main()
{
student s1;
s1.id=1;
s1.name="tth";
printf("id:%d",s1.id);
printf("name:%s",s1.name);
return 0;
}
输出: