有条件的操作员在C中给出汇编误差

发布于 2025-01-30 09:15:31 字数 59 浏览 4 评论 0原文

(a> b)?返回A:返回B;

此代码给出编译错误为什么

(a>b) ? return a : return b;

This code is giving compilation error why

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

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

发布评论

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

评论(3

暗地喜欢 2025-02-06 09:15:31

将条件运算符定义为以下方式,

logical-OR-expression ? expression : conditional-expression

该方式由三个表达式组成。

而不是第二个和第三个表达式

(a>b) ? return a : return b;

但是,您将语句返回 。因此,编译器会发出错误。

相反,您需要用包含条件运算符的表达式编写返回语句

return (a>b) ? a : b;

The conditional operator is defined the following way

logical-OR-expression ? expression : conditional-expression

That is it consists from three expressions.

However instead of the second and the third expressions

(a>b) ? return a : return b;

you placed the statement return. So the compiler issues an error.

Instead you need to write a return statement with an expression containing the conditional operator like

return (a>b) ? a : b;
扛刀软妹 2025-02-06 09:15:31

应该是:

返回(a> b)? A:B;

It should be:

return (a>b) ? a : b;

旧时模样 2025-02-06 09:15:31

返回未评估值,这是条件操作员操作数的明显要求。如果其操作数不评估值,则如何评估整个值?

用更多的技术术语,操作数必须是表达式。 返回A;将是一个有效的语句,但是返回不是有效的表达式,因为它没有评估值。这不是针对条件运算符(?:)的特定特定的。例如,x +返回yx&&返回y同样无效(在C中)。

您可以使用

return a>b ? a : b;

return a doesn't evaluate to a value, which is an obvious requirement of the conditional operator's operands. How can the whole evaluate to a value if its operands don't evaluate to a value?

In more technical terms, operands must be expressions. return a; would be a valid statement, but return a isn't a valid expression because it doesn't evaluate to a value. This isn't specific to the conditional operator (?:). For example, x + return y and x && return y are just as invalid (in C).

You could use

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