C 宏和括号中参数的使用

发布于 2024-12-01 14:01:30 字数 140 浏览 2 评论 0原文

示例

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单气质女生网名 2024-12-08 14:01:30

假设你有

#define mul(x, y)  x * y

如果我说:

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

现在如果我稍微更改宏:

#define mul(x, y)  ((x) * (y))
mul(a + 5, 6); /* ((a + 5) * (6)) */

请记住,不会评估参数或任何内容,只会执行文本替换。

编辑

有关将整个宏放在括号中的说明,请参阅链接 由 Nate CK 发布。

Suppose you have

#define mul(x, y)  x * y

What happens if I say:

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.

EDIT

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

黯淡〆 2024-12-08 14:01:30

仅供记录,我从这里登陆 如何在使用时修复数学错误宏,我将尝试在此处扩展此答案以适应另一个答案。

您询问的是以下差异:

#define Echo( a )  a
#define Echo( a ) ( a )

只要您不了解宏本身就可以(我也不是专家:))。

首先,您已经(可能)知道存在运算符优先级,因此这两个程序存在巨大差异:

1):

#include <stdio.h>
#define ADD( a , b ) a + b

int main( void )
{
    auto const int a = 5;
    auto const int b = 10;

    auto const int c = ADD (  2 + a ,  2 + b );
    printf( "%d", c );
    return 0;
}

输出:

19

和:

#include <stdio.h>
#define ADD( a , b ) ( a ) + ( b )

int main( void )
{
    auto const int a = 5;
    auto const int b = 10;

    auto const int c = ADD ( a , b );
    printf( "%d", c );
    return 0;
}

输出:

15

现在让我们预先替换 +*

#define ADD( a, b ) a * b

编译器将 a * b 视为 a == 5b == 10< /code> 执行 5 * 10..

但是,当你说:
添加(2 + a * 5 + b)
就像这里:

#include <stdio.h>
#define ADD( a , b ) ( a ) * ( b )

int main( void )
{
    auto const int a = 5;
    auto const int b = 10;

    auto const int c = ADD ( 2 + a , 5 + b );
    printf( "%d", c );
    return 0;
}

您得到 105,因为涉及运算符优先级,并将

2 + b * 5 + a

视为

( 2 + 5 ) * ( 5 + 10 )

( 7 ) * ( 15 ) == 105

但是当你这样做时:

#include <stdio.h>
#define ADD( a, b ) a * b

int main( void )
{
    auto const int a = 5;
    auto const int b = 10;

    auto const int c = ADD ( 2 + a , 5 + b );
    printf( "%d", c );
    return 0;
}

你会得到 37 因为

 2 + 5 * 5 + 10

这意味着:

2 + ( 5 * 5 ) + 10

哪个意思是:

2 + 25 + 10

简答,有很大的区别:

#define ADD( a , b ) a * b

#define ADD( a , b ) ( a ) * ( a )

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:

#define Echo( a )  a
#define Echo( a ) ( a )

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):

#include <stdio.h>
#define ADD( a , b ) a + b

int main( void )
{
    auto const int a = 5;
    auto const int b = 10;

    auto const int c = ADD (  2 + a ,  2 + b );
    printf( "%d", c );
    return 0;
}

Output:

19

and:

#include <stdio.h>
#define ADD( a , b ) ( a ) + ( b )

int main( void )
{
    auto const int a = 5;
    auto const int b = 10;

    auto const int c = ADD ( a , b );
    printf( "%d", c );
    return 0;
}

Output:

15

Now lets preplace + with *:

#define ADD( a, b ) a * b

The compiler treats a * b like for example a == 5 and b == 10 which does 5 * 10.

But, when you say:
ADD ( 2 + a * 5 + b )
Like here:

#include <stdio.h>
#define ADD( a , b ) ( a ) * ( b )

int main( void )
{
    auto const int a = 5;
    auto const int b = 10;

    auto const int c = ADD ( 2 + a , 5 + b );
    printf( "%d", c );
    return 0;
}

You get 105, because the operator precedence is involved and treats

2 + b * 5 + a

as

( 2 + 5 ) * ( 5 + 10 )

which is

( 7 ) * ( 15 ) == 105

But when you do:

#include <stdio.h>
#define ADD( a, b ) a * b

int main( void )
{
    auto const int a = 5;
    auto const int b = 10;

    auto const int c = ADD ( 2 + a , 5 + b );
    printf( "%d", c );
    return 0;
}

you get 37 because of

 2 + 5 * 5 + 10

which means:

2 + ( 5 * 5 ) + 10

which means:

2 + 25 + 10

Short answer, there is a big difference between:

#define ADD( a , b ) a * b

and

#define ADD( a , b ) ( a ) * ( a )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文