C如何评估比探索少?

发布于 2025-02-01 08:51:49 字数 132 浏览 2 评论 0原文

我想知道如何用C语言对此代码进行评估?

    int x = 10, y = 20, z = 5, i;
    i = x < y < z;
    printf("%d\n",i);

I would like to know how i is evaluated in this code in C language ?

    int x = 10, y = 20, z = 5, i;
    i = x < y < z;
    printf("%d\n",i);

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

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

发布评论

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

评论(1

若言繁花未落 2025-02-08 08:51:49

关系运算符的结果是整数1如果条件为真,则是0。关系运营商从左到右评估。

因此,此语句

i = x < y < z;

等同于

i = ( x < y ) < z;

x,因此x小于y,因此它也可以像1初始化变量i的初始化一样重写,

i = 1 < z;

1小于5。

因为

6每个操作员&lt; (小于),&gt; (大于),&lt; =(小于
或等于),并且&gt; =(大于或等于)如果
指定的关系为true,如果是错误的。107)结果具有
类型int。

如果您像这样重写语句

i = x < y && y < z;

,那么表达式的结果将等于0,因为y不小于z。

The result of a relational operator is either integer 1 if the condition is true or 0 otherwise. And relational operators evaluates from left to right.

So this statement

i = x < y < z;

is equivalent to

i = ( x < y ) < z;

and as x is less than y then it can be also rewritten like

i = 1 < z;

that initialize the variable i by 1 because 1 is less than 5.

From the C Standard (6.5.8 Relational operators)

6 Each of the operators < (less than), > (greater than), <= (less than
or equal to), and >= (greater than or equal to) shall yield 1 if the
specified relation is true and 0 if it is false.107) The result has
type int.

If you will rewrite the statement like

i = x < y && y < z;

then the result of the expression will be equal to 0 because y is not less than z.

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