在C语言中指定IF条件内的范围

发布于 2025-02-13 18:44:20 字数 358 浏览 3 评论 0原文

我只想知道这些C代码段之间的区别。我以两种不同的方式使用了它,最终输出不相同。

if(120<=units<160){
    
    total= total- total*(15.0/100);
    printf("Revenue : %.2f", total);
    
}


if(120<=units && units<160){
    
    total= total- total*(15.0/100);
    printf("Revenue : %.2f", total);
    
}

我们不能使用“ 120&lt; =单位&lt; 160”来指定C语言的范围?

I just want to know the difference between these C code segments. I used it in two different ways and final output was not same.

if(120<=units<160){
    
    total= total- total*(15.0/100);
    printf("Revenue : %.2f", total);
    
}


if(120<=units && units<160){
    
    total= total- total*(15.0/100);
    printf("Revenue : %.2f", total);
    
}

Can't we use "120<=units<160" to specify a range in C language?

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

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

发布评论

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

评论(3

流殇 2025-02-20 18:44:20

不,您不能以这种方式测试范围。这里发生的事情是按照操作员优先。

因此,这意味着:(120&lt; =单位)&lt; 160

120&lt; =单位的值如果为false或 1如果换句话说

,完整的表达将始终是正确的,因为0和1均小于160。

No, you can't test ranges in this way. What's happening here is the operators are evaluated left-to-right as per operator precedence.

So this means: (120 <= units) < 160

The value of 120 <= units will be 0 if it is false or 1 if it is true

In other words, the full expression will always be true, because both 0 and 1 are less than 160.

一个人的旅程 2025-02-20 18:44:20

它被解析为这两个(我不记得哪一个)之一:

  • (120&lt; = units)&lt; 160
  • 120&lt; =(单位&lt; 160)

在这两种情况下,都进行了一个比较,产生一个“ boolean”(01)与其他数字进行比较,通常会导致“错误”结果(嗯,这是完全正确的,只是不符合您的期望)

It's parsed as one of these two (I don't remember which):

  • (120 <= units) < 160
  • 120 <= (units < 160)

In both cases, one comparison is done, yielding a "boolean" (0 or 1) which is then compared with the other number, typically leading to a "wrong" result (well, it's perfectly correct, just doesn't match your expectation)

吹泡泡o 2025-02-20 18:44:20

这个问题类似于C ++的帖子。您可以参考下面链接中的帖子。 https://stackoverflow.com/a/3831148/19395482

考虑单个语句:bool结果= 18&lt;年龄&LT; 30;
我们想评估右手表达:18&lt;年龄&LT; 30

此表达式中有两个操作员,由于它们相同,因此它们都具有相同的优先级,因此,在这种情况下,它们从左到右进行了评估,因此该表达式等同于:

(18 < age) < 30

所以让我们先检查左手成员:18&lt;年龄,它产生的布尔值是真或错误的,通常表示为1或0的积分值。因此,可以将表达式求和为:

{0,1} < 30

始终是正确的。

因此,您是否应该使用断言(18&lt; age&lt; 30);它永远不会适得其反。

The question is similar to a post in C++. You can refer to the post in the link below. https://stackoverflow.com/a/3831148/19395482

By Matthieu M.:

Consider the single statement: bool result = 18 < age < 30;
We want to evaluate the right-hand expression: 18 < age < 30

There are two operators in this expression, and since they are identical, they both have the same priority, in this case, they are thus evaluated from left to right, therefore the expression is equivalent to:

(18 < age) < 30

So let's first examine the left-hand member: 18 < age, it yields a boolean which is either true or false, typically represented as an integral value respectively 1 or 0. Thus the expression can be summed up as:

{0,1} < 30

which is always true.

Therefore, should you use assert(18 < age < 30); it would never backfire.

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