相关疑难解决方法(0)

C中宏的括号需要

我试着SQR在下面的代码中使用宏的定义:

#define SQR(x) (x*x)
int main()
{
    int a, b=3;
    a = SQR(b+5);      // Ideally should be replaced with (3+5*5+3), though not sure.
    printf("%d\n",a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它打印23.如果我将宏定义更改为SQR(x) ((x)*(x))然后输出是预期的,64.我知道在C中调用宏会用宏的定义替换调用,但是我仍然无法理解它是如何计算的23.

c macros parentheses c-preprocessor

28
推荐指数
4
解决办法
2万
查看次数

C宏和括号中的参数使用

#define Echo(a)  a
#define Echo(a) (a)
Run Code Online (Sandbox Code Playgroud)

我意识到这里可能没有显着差异,但为什么你想要a在宏体内包括括号内?它是如何改变它的?

c macros parentheses c-preprocessor

13
推荐指数
1
解决办法
3032
查看次数

标签 统计

c ×2

c-preprocessor ×2

macros ×2

parentheses ×2