C 预处理器和操作顺序

发布于 2024-11-03 23:45:53 字数 151 浏览 3 评论 0原文

我正在学习 C,但我不明白这一点:

#define square(x) x*x
a = square(2+3) //a = 11

运行时,为什么 a 最终会变成 11

I'm learning C, but I do not understand this:

#define square(x) x*x
a = square(2+3) //a = 11

When this is run, why does a end up being 11?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

星光不落少年眉 2024-11-10 23:45:53

它扩展为2+3*2+3,相当于2+(3*2)+3。使用括号来修复它:

#define square(x) ((x)*(x))

现在尝试使用 square(x++) ,您将遇到更多问题(未定义的行为)。如果可以的话,请避免将其作为宏来执行。

It expands to 2+3*2+3, which is equivalent to 2+(3*2)+3. Use parentheses to fix it:

#define square(x) ((x)*(x))

Now try it with square(x++) and you'll run into more problems (undefined behavior). Avoid doing this as a macro if you can.

各自安好 2024-11-10 23:45:53

square(2+3) 扩展为 2+3*2+3 相当于 2+(3*2)+3 ( * 的优先级高于 +)。

GCC中,您可以使用-E 选项来查看预处理器生成的内容:

cd "C:\Users\SUPER USER"
type a.c

输出:

#define square(x) x*x

int main()
{
   a = square(2+3); //a = 11
}

C:\Users\SUPER USER>gcc -E a.c
# 1 "a.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "a.c"


int main()
{
   a = 2+3*2+3;
}

补救措施

试试这个:

#define square(x) ((x)*(x))

square(2+3) expands to 2+3*2+3 which is equivalent to 2+(3*2)+3 (* has higher precedence than +).

In GCC, you can use -E option to see what your preprocessor generates:

cd "C:\Users\SUPER USER"
type a.c

Output:

#define square(x) x*x

int main()
{
   a = square(2+3); //a = 11
}

C:\Users\SUPER USER>gcc -E a.c
# 1 "a.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "a.c"


int main()
{
   a = 2+3*2+3;
}

Remedy

Try this:

#define square(x) ((x)*(x))
没有心的人 2024-11-10 23:45:53

尝试:

#define square(x) ((x)*(x))

Try:

#define square(x) ((x)*(x))
森罗 2024-11-10 23:45:53

由于 2 + 3 在表达式 x * x 中按字面替换,因此它变为 2 + 3 * 2 + 3,并且 * 运算符具有更高的优先级,因此您不会得到预期的结果。

始终将宏参数和整个表达式括在括号中以避免这种情况:

#define SQUARE(x) ((x) * (x))

另请注意,您传递的任何表达式都将被计算两次,如果表达式具有副作用(例如赋值或函数调用),则这可能是不希望的。在这些情况下,最好使用内联函数。

Because 2 + 3 is substituted literally in the expression x * x, it becomes 2 + 3 * 2 + 3, and the * operator has a higher precedence so you don't get the expected result.

Always enclose macro arguments and the whole expression in parentheses to avoid this:

#define SQUARE(x) ((x) * (x))

Also note that any expression you pass will be evaluated twice, and that can be undesired if the expression has a side effect such as an assignment, or a function call. In these cases it is better to use an inline function.

仙女山的月亮 2024-11-10 23:45:53

想想当宏展开时你会得到什么。 C 预处理器将扩展它,因为

a = 2 + 3 * 2 + 3

您需要正确定义宏。始终将宏变量括在括号内。这会给你预期的结果。

#define square(x) ((x)*(x))

宏展开式是这样的:

a = ((2 + 3) * (2 + 3))

Think about what you get when the macro is expanded. The C preprocessor will expand this as

a = 2 + 3 * 2 + 3

You need to correctly define your macro. Always enclose the macro variable in parentheses. This would give you the expected result.

#define square(x) ((x)*(x))

The macro expansion would be this:

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