C语言宏定义-#和##在宏定义中的作用

C语言宏定义-#和##在宏定义中的作用

1. 宏#

#的作用是把宏的标识符进行字符串化操作(Stringfication),简单说就是在对它所引用的宏变量通过替换后在其左右各加上一个双引号

/**
 * @def CMO_STRINGIFY
 * @brief converts a macro argument into a character constant.
 *
 * @param[in] x - A macro argument to stringify.
 */
#define CMO_STRINGIFY(x) CMO_STRINGIFY_(x)
/** Auxiliary macro used by @ref CMO_STRINGIFY */
#define CMO_STRINGIFY_(x) #x

2. 宏##

##被称为连接符(concatenator),用来将两个标识符连接为一个标识符

/**
 * @def CMO_CONCAT2
 * @brief Concatenates two parameters.
 *
 * It realizes two level expansion to make it sure that all the parameters
 * are actually expanded before gluing them together.
 *
 * @param[in] p1 - First parameter to concatenating
 * @param[in] p2 - Second parameter to concatenating
 *
 * @return Two parameters glued together.
 *         They have to create correct C mnemonic in other case
 *         preprocessor error would be generated.
 */
#define CMO_CONCAT2(p1, p2) CMO_CONCAT2_(p1, p2)
/** Auxiliary macro used by @ref CMO_CONCAT2 */
#define CMO_CONCAT2_(p1, p2) p1##p2

/**
 * @def CMO_CONCAT3
 * @brief Concatenates three parameters.
 *
 * It realizes two level expansion to make it sure that all the parameters
 * are actually expanded before gluing them together.
 *
 * @param[in] p1 - First parameter to concatenating
 * @param[in] p2 - Second parameter to concatenating
 * @param[in] p3 - Third parameter to concatenating
 *
 * @return Three parameters glued together.
 *         They have to create correct C mnemonic in other case
 *         preprocessor error would be generated.
 */
#define CMO_CONCAT3(p1, p2, p3) CMO_CONCAT3_(p1, p2, p3)
/** Auxiliary macro used by @ref CMO_CONCAT3 */
#define CMO_CONCAT3_(p1, p2, p3) p1##p2##p3
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值