C语言#和##运算符的用法

#运算符
#运算符用于在预编译期将宏参数转换为字符串
#define CONVERS(x) #x
printf(‘%s\n”,CONVERS(Hello world!));

CONVERS(100)
CONVERS(while)

#define CALL(f,p) (printf(“Call function %s”,#f),f(p))
int square(int n)
{
return n*n;
}
int f(in x)
{
return x;
}
int main()
{
printf(“1. %d\n”,CALL(square,4));
printf(“2.%d\n”,CALL(f,10));
return 0;
}

运算符

运算符用于在预编译期粘连两个符号

include

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;
}

利用##定义结构体类型
#define STRUCT(type) typedef struct_tag_##type type;\
struct tag##type

STRUCT(Student)
{
char * name;
int id;
}
这里写图片描述

这样就不用写6个函数了

猜你喜欢

转载自blog.csdn.net/xujaiwei/article/details/78876795
今日推荐