首页 > 编程 > C++ > 正文

C++/C 宏定义(define)中#和## 的含义

2019-11-08 03:16:06
字体:
来源:转载
供稿:网友

#是字符串化的意思,出现在宏定义中的#是把跟在后面的参数转成一个字符串;

##是连接符号,把参数连接在一起。

例子:

#include <iostream>using namespace std;#define A(exp) cout << "I am "#exp << endl;#define B(exp) cout << sz##exp << endl;int main(){    const char *szStr = "test";    A(Chinese); // cout << "I am ""Chinese" << endl;    B(Str);     // cout << szStr << endl;    return 0;}
#include <iostream>using namespace std;#define  OUTPUT(A) cout<<#A<<":"<<(A)<<endl;#define  CONCAT(A,B) A##Bint main(){    int a=1,b=2;    OUTPUT(a);          //a:1    OUTPUT(b);          //b:2    OUTPUT(a+b);        //a+b:3    cout<<CONCAT(1,2);  //12    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选