C 宏和括号中参数的使用 [英] C macros and use of arguments in parentheses

查看:24
本文介绍了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

如果我说:

mul(a + 5, 6); /* a + 5 * 6 */

现在如果我稍微改变宏:

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.

有关在括号中包含整个宏的说明,请参阅链接Nate CK 发布.

For an explanation about having the entire macro in parentheses, see the link posted by Nate C-K.

这篇关于C 宏和括号中参数的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆