1、#运算符用于在预编译期将宏参数转换为字符串
#include <stdio.h>
#define CONVERS(x) #x
int main()
{
printf ("%s\n", CONVERS(Hello World!));
printf ("%s\n", CONVERS(100));
printf ("%s\n", CONVERS(while));
printf ("%s\n", CONVERS(return));
return 0;
}
2、##运算符用于在预编译期粘连两个符号
#include <stdio.h>
#define NAME(n) name##n
int main()
{
int NAME(1);
int NAME(2);
NAME(1) = 1;
NAME(2) = 2;
printf("%d\n", NAME(1));
printf("%d\n", NAME(2));
return 0;
}
#include <stdio.h>
#define CONVERS(x) #x
int main()
{
printf ("%s\n", CONVERS(Hello World!));
printf ("%s\n", CONVERS(100));
printf ("%s\n", CONVERS(while));
printf ("%s\n", CONVERS(return));
return 0;
}
2、##运算符用于在预编译期粘连两个符号
#include <stdio.h>
#define NAME(n) name##n
int main()
{
int NAME(1);
int NAME(2);
NAME(1) = 1;
NAME(2) = 2;
printf("%d\n", NAME(1));
printf("%d\n", NAME(2));
return 0;
}
3、利用##定义结构体类型
#include <stdio.h>
#define STRUCT(type) typedef struct _tag_##type type;\
struct _tag_##type
STRUCT(Student)
{
char* name;
int id;
};
int main()
{
Student s1;
Student s2;
s1.name = "s1";
s1.id = 0;
s2.name = "s2";
s2.id = 1;
printf("%s\n", s1.name);
printf("%d\n", s1.id);
printf("%s\n", s2.name);
printf("%d\n", s2.id);
return 0;
}
本文介绍了C语言中预编译器的高级用法,包括使用#运算符将宏参数转换为字符串,使用##运算符在预编译期间连接两个符号,以及如何利用这些特性定义结构体类型。
1148

被折叠的 条评论
为什么被折叠?



