【发布时间】:2020-05-15 05:02:49
【问题描述】:
在下面的宏定义中,“#”符号有什么作用?这里的语法(#x)是什么?
#define print(x) cout<<(#x)<<" : "<<x<<endl;
【问题讨论】:
在下面的宏定义中,“#”符号有什么作用?这里的语法(#x)是什么?
#define print(x) cout<<(#x)<<" : "<<x<<endl;
【问题讨论】:
# 是字符串化操作符。它将宏参数x 转换为字符串文字。
我看不出有多余的括号,#define print(x) cout << #x <<" : " << x << endl; 也可以。 #define print(x) cout << #x <<" : " << (x) << endl; 更好,因为第二次使用 x 可能需要正确解析括号。
【讨论】: