用C复合条件:如果(0.0&LT; A&LT; 1.0) [英] Compound condition in C: if (0.0 < a < 1.0)
问题描述
最近,我注意到下面的前pression在我的编译器(锵)编译:
I recently noticed that the following expression compiles in my compiler (Clang):
float a;
if (0.0 < a < 1.0) { ... }
这是否做什么,我期待?我希望它相同的计算结果为这种情况:
Does this do what I expect? I would expect that it evaluates identically to this condition:
if (0.0 < a && a < 1.0) { ... }
如果是这样,因为当与哪些其他的编译器就可以编写条件在这短短的形式?
If it is so, since when and with which other compilers it is possible to write conditions in this short form?
推荐答案
左到右的关联&LT;
运营商前pression条件(0.0&LT; A&LT; 1.0)
办法((0.0&LT;一)LT; 1.0)
== 1 LT; 1.0
或 0℃; 1.0
具体取决于值 A
。
Because of left-to-right associativity of <
operator the expression condition (0.0 < a < 1.0)
means ((0.0 < a) < 1.0)
== 1 < 1.0
or 0 < 1.0
depending on value of a
.
所以,不,它不是等同于如果(0.0&LT; A和&功放; A&LT; 1.0)
(也许你会从Python的同情规则混淆),但在C时会如我上面解释的相互渗透。
So no, its not identical to if (0.0 < a && a < 1.0)
(perhaps you might confusing from Python compassion rule) but in C it will be interpenetrated as I explained above.
有一个区别,你可以在一个例子---观察 0.0&LT; A&LT; 1.0
== 真正
在 A
== 0.0
,其中以(0.0&LT; A和&功放; A&LT; 1.0)
== false,那么 A
== 0.0
,下面是我的code(阅读评论)
A difference you can observe in an example --- 0.0 < a < 1.0
== true
when a
== 0.0
, where as (0.0 < a && a < 1.0)
== false, for a
== 0.0
, below is my code (read comments):
#include<stdio.h>
void print_(int c){
c ? printf("True \n"):
printf("False \n");
}
int main(void){
float a = 0.0f;
print_(0.0f < a < 1.0f); // 0.0 < 0.0 < 1.0f == 0 < 1.0f == True
print_(0.0f < a && a < 1.0f); // 0.0f < 0.0f && ... == False && ... = False
return 0;
}
输出:
True
False
检查其工作@ Ideone
这篇关于用C复合条件:如果(0.0&LT; A&LT; 1.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!