相关疑难解决方法(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 ×1

c-preprocessor ×1

macros ×1

parentheses ×1