有关宏定义传入宏参数和##参数

宏本质为内容的替换
带有参数的宏赋予其内容的灵活性
而在宏定义中使用宏 赋予宏内容更深层的展开能力
下文示例中展示

  • 宏定义中使用多个宏
  • 将宏作为参数传入宏
  • 宏参数的拼接##
  • 宏展开后形成另一个宏
    等多种特殊情况

宏的定义 包含 名称参数框和内容
其中 宏的内容中递归处理内容中的宏 而宏的定义是刻板的字段
以上没有争议

而宏的参数以及 参数拼接(参数##参数)相对容易引发问题
其复杂性体现于 参数拼接出现的位置
当宏的定义中使用了多个带有参数的宏
参数可能由外向内主机传递
如果传入的参数本身也是一个宏 并且在传入其它宏时候使用##进行了字段拼接
则这个参数(宏)会使用其名称而非内容进入拼接 也就是没有按照使用者的预期展开替换

//
// Created by dick spin
//

#ifndef INTAN_RHD_TO_STM32_NUCLEO5A5ZJ_MCRO_NOTE_H
#define INTAN_RHD_TO_STM32_NUCLEO5A5ZJ_MCRO_NOTE_H

#endif //INTAN_RHD_TO_STM32_NUCLEO5A5ZJ_MCRO_NOTE_H

#include "stdint.h"
#define  tast TAST

#define  DEF_FUNNAME_LOAD(regIndex)  uint32_t load_parm_to_register##regIndex(uint32_t i)
#define  DEF_FUNIMP_LOAD(regIndex,tast) {      \
printf("%s\n",tastregIndex);                   \
printf("%s\n",tast regIndex);                  \
printf("%s\n",tast##regIndex);                  \
if(i== regIndex) {retrun 0;}return 1;}

#define  FUN_COMBINE_tast(regIndex ,tast)   DEF_FUNNAME_LOAD(regIndex)  DEF_FUNIMP_LOAD(regIndex,tast) 
#define  tastI(I) TASTI##I

FUN_COMBINE_tast(10,tast)
//uint32_t load_parm_to_register10(uint32_t i) {
//    printf("%s\n", tastregIndex);
//    printf("%s\n", TAST
//    10);
//    printf("%s\n", TAST10);
//    if (i == 10) {
//        retrun
//        0;
//    }
//    return 1;
//}

FUN_COMBINE_tast(10,tast##10)
//uint32_t load_parm_to_register10(uint32_t i) {
//    printf("%s\n", tastregIndex);
//    printf("%s\n", TAST##10
//    10);
//    if (i == 10) {
//        retrun
//        0;
//    }
//    return 1;
//}

FUN_COMBINE_tast(10,tast##tast)
//uint32_t load_parm_to_register10(uint32_t i) {
//    printf("%s\n", TAST##TAST);
//    printf("%s\n", TAST##TAST10);
//    if (i == 10) {
//        retrun
//        0;
//    }
//    return 1;
//}

FUN_COMBINE_tast(10,tastI(10))
//uint32_t load_parm_to_register10(uint32_t i) {
//    printf("%s\n", tastregIndex);
//    printf("%s\n", TASTI10
//    10);
//    if (i == 10) {
//        retrun
//        0;
//    }
//    return 1;
//}

#define  TAST  z

FUN_COMBINE_tast(10,tast##tast)
//uint32_t load_parm_to_register10(uint32_t i) {
//    printf("%s\n", tastregIndex);
//    printf("%s\n", z##z
//    10);
//    printf("%s\n", z##z10);
//    if (i == 10) {
//        retrun
//        0;
//    }
//    return 1;
//}

 
#define  FUN_COMBINE_tast(regIndex ,tast)   DEF_FUNNAME_LOAD(regIndex)  DEF_FUNIMP_LOAD(regIndex,tast##a) 
//传入tast 为宏定义 展开后TAST 依然为宏定义 z
//注释部分为宏的展开 这里能够发现 多层定义的宏 使用##拼接参数 参数如果是宏定义 将使用宏的名称字段而非宏的内容
FUN_COMBINE_tast(1,tast)
//uint32_t load_parm_to_register1(uint32_t i) {
//    printf("%s\n", tastregIndex);
//    printf("%s\n", tasta
//    1);
//    printf("%s\n", tasta1);
//    if (i == 1) {
//        retrun
//        0;
//    }
//    return 1;
//}
#define  FUN_COMBINE_tast(regIndex ,tast)   DEF_FUNNAME_LOAD(regIndex)  DEF_FUNIMP_LOAD(regIndex,tast##regIndex)
//uint32_t load_parm_to_register1(uint32_t i) {
//    printf("%s\n", tastregIndex);
//    printf("%s\n", tasta
//    1);
//    printf("%s\n", tasta1);
//    if (i == 1) {
//        retrun
//        0;
//    }
//    return 1;
//}
```c
在这里插入代码片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值