尝试三个数字比较与三元操作员不起作用

发布于 2025-01-23 03:11:17 字数 742 浏览 0 评论 0原文

#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 技术交流群。

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

发布评论

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

评论(1

寻找一个思念的角度 2025-01-30 03:11:17

?:的优先级高于=。您应该这样做:

better_name_than_l = (n1 >= n2) ? n1 : ((n1 >= n3) ? n1 : ((n2 >= n3) ? n2 : n3))

作为奖励,代码从“非常不可读的混乱”变成了“有点不可读的混乱”。

l(l)是变量的一个不可思议的坏名称,因为它看起来像1(一个)在许多字体上 - 以至于像Misra C这样的安全标准明确具有禁止l作为变量名称。

另外,由于我们已经确定我们讨厌可读的代码(这是用于代码打高尔夫球,对吗

better_name_than_l = n1 >= n2 ? n1 : n1 >= n3 ? n1 : n2 >= n3 ? n2 : n3;

=

?: has higher precedence than =. You should do:

better_name_than_l = (n1 >= n2) ? n1 : ((n1 >= n3) ? n1 : ((n2 >= n3) ? n2 : n3))

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 like 1 (one) on many fonts - to the point where safety standards like MISRA C have explicitly banned l 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:

better_name_than_l = n1 >= n2 ? n1 : n1 >= n3 ? n1 : n2 >= n3 ? n2 : n3;

Precedence is >= over ?: over =.

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