C++中#和##

转自:https://blog.csdn.net/YhL_Leo/article/details/48879093

1.介绍 

C/C++ 的宏中, 

  • #的功能是将其后面的宏参数进行字符串化操作;
  • ## 功能是在带参数的宏定义中将两个子串联接起来,从而形成一个新的子串。但它不可以是第一个或者最后一个子串。 

凡是宏定义里有用###的地方宏参数是不会再展开,

#include <iostream>
using namespace std;

#define WARN_IF(EXP) if(EXP) cerr << #EXP << endl;
#define paster( n ) cout << "token" << #n << " = " << n << endl;
#define _CONS(a, b) int(a##+##b)
#define _STRI(s) #s

int main()
{
    int div = 0;
    WARN_IF(div == 0);           // prints : div == 0
    paster(9);                   // prints : token9 = 9
    // cout << _CONS(1, 2) << endl;     // pasting "1" and "+" does not give a valid preprocessing token
    cout << _STRI(INT_MAX) << endl;  // prints : INT_MAX
    return 0;
}

输出:

div == 0
token9 = 9
INT_MAX

2. ## 例子  

https://blog.csdn.net/xdsoft365/article/details/5911596

## (token-pasting)符号连接操作符。

#define exampleNum(n) num##n
int num9=9;

int num=exampleNum(9); //将会扩展成 int num=num9;

上述例子中,num##n即num9,已经定义了num9,所以num值被初始化为9。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值