C-#与##

C-#与##

  • Point1 (#)
    • #符号用作一个预处理运算符,可以把宏参数转换为字符串,
    • 我们可能有下面这个需求
        #define Squre1(x) printf("The square of " #x" is %d \n", ((x)*(x)));
        #define Squre2(x) printf("The square of x is %d \n", ((x)*(x)));

        int main()
        {

            int y = 5;

            Squre1(y);  //打印结果是: The square of y is 25; 
            Squre2(y);   // The square of x is 25;

                        //即, 我们把宏参数,嵌入到了字符串中, 该过程称为参数字符窜化 
            return 0;
        }
  • Point2 (##)
    • ##符号用于在编译器连接两个符号
    • 例如:
    #define NAME(n)  name##n
        -> NAME(1)   //name1;    
- 上面这个例子可能没什么意思,来看一下下面这个例子对##的妙用
- 你能理解吗?
    //利用##定义结构体类型
        #define STRUCT(type) typedef struct _tag_##type type;\
        struct _tag_##type


        STRUCT(Student){
            char* name;
            int id;
        };

        /*
        typedef struct _tag_Student Student; 
        struct _tag_Student{
            char* name;
            int id; 
        };

        */


        int main()
        {
            Student s1;
            s1.name="小明";

            printf("%s", s1.name);
            return 0;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值