C宏并在括号中使用的参数 [英] C macros and use of arguments in parentheses
本文介绍了C宏并在括号中使用的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例
#define Echo(a) a
#define Echo(a) (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?
推荐答案
假设你有
#define mul(x, y) x * y
会发生什么,如果我说:
What happens if I say:
mul(a + 5, 6); /* a + 5 * 6 */
现在,如果我slighlty更改宏:
Now if I slighlty change the macro:
#define mul(x, y) ((x) * (y))
mul(a + 5, 6); /* ((a + 5) * (6)) */
记住,参数不评估或任何东西,只进行文本替换。
Remember, the arguments aren't evaluated or anything, only textual substitution is performed.
For an explanation about having the entire macro in parentheses, see the link posted by Nate C-K.
这篇关于C宏并在括号中使用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文