有条件的操作员在C中给出汇编误差
(a> b)?返回A:返回B;
此代码给出编译错误为什么
(a>b) ? return a : return b;
This code is giving compilation error why
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
(a> b)?返回A:返回B;
此代码给出编译错误为什么
(a>b) ? return a : return b;
This code is giving compilation error why
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
将条件运算符定义为以下方式,
该方式由三个表达式组成。
而不是第二个和第三个表达式
但是,您将语句
返回
。因此,编译器会发出错误。相反,您需要用包含条件运算符的表达式编写返回语句
The conditional operator is defined the following way
That is it consists from three expressions.
However instead of the second and the third expressions
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
应该是:
返回(a> b)? A:B;
It should be:
return (a>b) ? a : b;
返回
未评估值,这是条件操作员操作数的明显要求。如果其操作数不评估值,则如何评估整个值?用更多的技术术语,操作数必须是表达式。
返回A;
将是一个有效的语句,但是返回
不是有效的表达式,因为它没有评估值。这不是针对条件运算符(?:
)的特定特定的。例如,x +返回y
和x&&返回y
同样无效(在C中)。您可以使用
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, butreturn 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
andx && return y
are just as invalid (in C).You could use