C语言 链接多个大于/小于运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6961643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chaining multiple greater than/less than operators
提问by jscode
In an ifstatement I want to include a range, e.g.:
在if声明中,我想包含一个范围,例如:
if(10 < a < 0)
but by doing so, I get a warning "Pointless comparison". However, this works fine without any warning:
但是这样做,我会收到警告“毫无意义的比较”。但是,这可以正常工作而没有任何警告:
if(a<10 && a>0)
Is the first case possible to implement in C?
第一种情况可以在 C 中实现吗?
回答by Keith Thompson
Note that the original version if(10 < a < 0)is perfectly legal. It just doesn't do what you might (reasonably) think it does. You're fortunate that the compiler recognized it as a probable mistake and warned you about it.
请注意,原始版本if(10 < a < 0)是完全合法的。它只是不会做您可能(合理地)认为它会做的事情。您很幸运,编译器将其识别为一个可能的错误并警告您。
The <operator associates left-to-right, just like the +operator. So just as a + b + creally means (a + b) + c, a < b < creally means (a < b) < c. The <operator yields an int value of 0 if the condition is false, 1 if it's true. So you're either testing whether 0 is less than c, or whether 1 is less than c.
该<运营商联营左到右,就像+操作。所以就像a + b + c真的意思一样(a + b) + c,a < b < c真的意思(a < b) < c。所述<,如果条件为假,1,如果它是真实的操作者产生的0 int值。因此,您要么测试 0 是否小于 c,要么测试 1 是否小于 c。
In the unlikely case that that's really what you want to do, adding parentheses will probably silence the warning. It will also reassure anyone reading your code later that you know what you're doing, so they don't "fix" it. (Again, this applies only in the unlikely event that you reallywant (a < b) < c).)
在不太可能的情况下,这确实是您想要做的,添加括号可能会使警告静音。它还可以让任何稍后阅读您的代码的人放心,您知道自己在做什么,因此他们不会“修复”它。(同样,这仅适用于您真正想要的不太可能发生的事件(a < b) < c)。)
The way to check whether ais less than band bis less than cis:
检查是否a小于b和b小于的方法c是:
a < b && b < c
(There are languages, including Python, where a < b < cmeans a<b && b<c, as it commonly does in mathematics. C just doesn't happen to be one of those languages.)
(有些语言,包括 Python,其中的a < b < c意思是a<b && b<c,正如它在数学中通常所做的那样。C 只是碰巧不是这些语言之一。)
回答by user703016
It's not possible, you have to split the check as you did in case 2.
这是不可能的,您必须像在案例 2 中那样拆分支票。
回答by Alok Save
No it is not possible.
You have to use the second way by splitting the two conditional checks.
不,这是不可能的。
您必须通过拆分两个条件检查来使用第二种方式。
回答by Jerry Coffin
The first does one comparison, then compares the result of the first to the second value. In this case, the operators group left to right, so it's equivalent to (10<a) < 0. The warning it's giving you is really because <will always yield 0 or 1. The warning is telling you that the result of the first comparison can never be less than 0, so the second comparison will always yield false.
第一个进行一次比较,然后将第一个的结果与第二个值进行比较。在这种情况下,运算符从左到右分组,因此等效于(10<a) < 0。它给你的警告真的是因为<总是会产生 0 或 1。警告告诉你第一次比较的结果永远不会小于 0,所以第二次比较总是会产生假。
Even though the compiler won't complain about it, the second isn't really much improvement. How can a number be simultaneously less than 0, but greater than 10? Ideally, the compiler would give you a warning that the condition is always false. Presumably you want 0<a<10and a>0 && a<10.
即使编译器不会抱怨它,第二个也没有太大的改进。一个数怎么可能同时小于0但大于10?理想情况下,编译器会警告您条件始终为假。想必你想要0<a<10和a>0 && a<10。
You can get the effect of the second using only a single comparison: if ((unsigned)a < 10)will be true only if the number is in the range 0..10. A range comparison can normally be reduced to a single comparison with code like:
您可以仅使用单个比较来获得第二个效果:if ((unsigned)a < 10)仅当数字在 0..10 范围内时才会为真。范围比较通常可以简化为使用以下代码进行的单个比较:
if ((unsigned)(x-range_start)<(range_end-range_start))
// in range
else
// out of range.
At one time this was a staple of decent assembly language programming. I doubt many people do it any more though (I certainly don't as a rule).
曾几何时,这是体面的汇编语言编程的主要内容。我怀疑很多人都不再这样做了(我当然不会这样做)。
回答by Steve
As stated above, you have to split the check. Think about it from the compiler's point of view, which looks at one operator at a time. 10 < a = True or False. And then it goes to do True/False < 0, which doesn't make sense.
如上所述,您必须拆分支票。从编译器的角度考虑,它一次查看一个操作符。10 < a = 真或假。然后它会做 True/False < 0,这是没有意义的。
回答by nobalG
no,this is not valid syntax of if statement,it should have a valid constant expression,or may have logical operators in them,and is executed only,when the expression in the bracket evaluates to true,or non zero value
不,这不是 if 语句的有效语法,它应该有一个有效的常量表达式,或者其中可能有逻辑运算符,并且仅当括号中的表达式计算为真或非零值时才执行

