尝试三个数字比较与三元操作员不起作用
#include <stdio.h>
void main()
{
int n1, n2, n3, l;
printf("Enter any three numbers:\n");
scanf("%d %d %d", &n1, &n2, &n3);
(n1 >= n2) ? l = n1 : ((n1 >= n3) ? l = n1 : ((n2 >= n3) ? l = n2 : l = n3));
printf("Largest number = %d", l);
}
说:“左派分配需要的lvalue”
编辑:
#include <stdio.h>
void main()
{
int n1, n2, n3, lar;
printf("Enter any three numbers:\n");
scanf("%d %d %d", &n1, &n2, &n3);
lar = n1 >= n2 ? n1 : n2;
lar = lar >= n3 ? lar : n3;
printf("Largest number = %d", lar);
}
这是一个作品,除了必须分解为2个零件
#include <stdio.h>
void main()
{
int n1, n2, n3, l;
printf("Enter any three numbers:\n");
scanf("%d %d %d", &n1, &n2, &n3);
(n1 >= n2) ? l = n1 : ((n1 >= n3) ? l = n1 : ((n2 >= n3) ? l = n2 : l = n3));
printf("Largest number = %d", l);
}
says "lvalue required as left operandof assignment"
edited:
#include <stdio.h>
void main()
{
int n1, n2, n3, lar;
printf("Enter any three numbers:\n");
scanf("%d %d %d", &n1, &n2, &n3);
lar = n1 >= n2 ? n1 : n2;
lar = lar >= n3 ? lar : n3;
printf("Largest number = %d", lar);
}
this one works except had to break down to 2 parts
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
?:
的优先级高于=
。您应该这样做:作为奖励,代码从“非常不可读的混乱”变成了“有点不可读的混乱”。
l
(l)是变量的一个不可思议的坏名称,因为它看起来像1
(一个)在许多字体上 - 以至于像Misra C这样的安全标准明确具有禁止l
作为变量名称。另外,由于我们已经确定我们讨厌可读的代码(这是用于代码打高尔夫球,对吗
?
=
。?:
has higher precedence than=
. You should do:As a bonus, the code went from "very unreadable mess" to just "slightly unreadable mess".
l
(L) is an incredibly bad name for a variable, since it looks like1
(one) on many fonts - to the point where safety standards like MISRA C have explicitly bannedl
as a variable name.Also, since we've already established that we hate readable code (this is for code golfing, right?) we can even drop the inner parenthesis everywhere:
Precedence is
>=
over?:
over=
.