C 宏和括号中参数的使用
示例
#define Echo(a) a
#define Echo(a) (a)
我意识到这里可能没有显着差异,但是为什么您想要在宏体内的括号内包含 a
?它如何改变它?
Example
#define Echo(a) a
#define Echo(a) (a)
I realize there probably isn’t a significant difference here, but why would you ever want to include the a
within parenthesis inside the macro body? How does it alter it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设你有
如果我说:
现在如果我稍微更改宏:
请记住,不会评估参数或任何内容,只会执行文本替换。
编辑
有关将整个宏放在括号中的说明,请参阅链接 由 Nate CK 发布。
Suppose you have
What happens if I say:
Now if I slighlty change the macro:
Remember, the arguments aren't evaluated or anything, only textual substitution is performed.
EDIT
For an explanation about having the entire macro in parentheses, see the link posted by Nate C-K.
仅供记录,我从这里登陆 如何在使用时修复数学错误宏,我将尝试在此处扩展此答案以适应另一个答案。
您询问的是以下差异:
只要您不了解宏本身就可以(我也不是专家:))。
首先,您已经(可能)知道存在运算符优先级,因此这两个程序存在巨大差异:
1):
输出:
和:
输出:
15
现在让我们预先替换
+
与*
:编译器将
a * b
视为a == 5
和b == 10< /code> 执行
5 * 10.
.但是,当你说:
添加(2 + a * 5 + b)
就像这里:
您得到
105
,因为涉及运算符优先级,并将2 + b * 5 + a
视为
( 2 + 5 ) * ( 5 + 10 )
即
( 7 ) * ( 15 )
==105
但是当你这样做时:
你会得到
37
因为这意味着:
哪个意思是:
简答,有很大的区别:
和
Just for the record, I landed from Here How to fix mathematical errors while using macros and I will try to expand this Answer here to fit the Other one.
You are asking about the difference about:
which is fine as long as you do not understand the macro it self (I am not an expert too :) ).
First of all you already (probably) know that there is Operator Precedence, so there is a huge difference of this two programs:
1):
Output:
and:
Output:
15
Now lets preplace
+
with*
:The compiler treats
a * b
like for examplea == 5
andb == 10
which does5 * 10
.But, when you say:
ADD ( 2 + a * 5 + b )
Like here:
You get
105
, because the operator precedence is involved and treats2 + b * 5 + a
as
( 2 + 5 ) * ( 5 + 10 )
which is
( 7 ) * ( 15 )
==105
But when you do:
you get
37
because ofwhich means:
which means:
Short answer, there is a big difference between:
and