C语言-------------#与##运算符使用分析

本文介绍了C语言中#和##运算符的作用,分别在预编译期处理宏参数转字符串和在编译期粘合符号。通过实例展示了如何使用它们创建结构体和调用函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#运算符

1、#运算符用于在预编译期将宏参数转换为字符串

#include <stdio.h>

#define  message(X)   #X

#define  CALL(f,p) (printf("This is fuction %s\n",#f),f(p))

int square (int n)
{
  return n*n;
}

int main()
{
    printf("%s\r\n",message(hello));
    printf("大家猜一猜这句是怎么打印的 %d\n",CALL(square,4));

}

2、##预算符用于在编译期粘连两个符号

很好的示例(来源于网络)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哆啦哆小魔仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值