比较操作符的数学般的链接 - 同在,与QUOT;若(1 LT; A< = 5)QUOT; [英] Math-like chaining of the comparison operator - as in, "if (1<A<=5)"

查看:83
本文介绍了比较操作符的数学般的链接 - 同在,与QUOT;若(1 LT; A< = 5)QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int j=42;
if(1<j<=5) {
    printf("yes");
} else {
    printf("no");
}

输出:

yes

为什么它的输出是吗?结果
是不是条件只有一半是真的吗?

Why does it output yes?
Isn't the condition only half true?

推荐答案

C不理解数学的语法,所以

C does not understand math-like syntax, so

if(1<j<=5)

如您所愿,想不跨preTED;它应该是

is not interpreted as you expect and want; it should be

if (1 < j && j <= 5)

或相似的。

在其他的答案解释,前pression被评为

As explained in other answers, the expression is evaluated as

 ((1 < j) <= 5)

 =>  ("true" <= 5)

 =>  "true"

其中,真(布尔值)隐式转换为1,explaneid例如这里,具有标准也引用了,这解释了为什么真必须少比5(虽然用C可能没有完全正确的谈论,从布尔隐式转换为int)

where "true" (boolean value) is implicitly converted to 1, as explaneid e.g. here, with references to standards too, and this explain why "true" has to be "less than" 5 (though in C might not be totally correct to speak about "implicit conversion from bool to int")

这篇关于比较操作符的数学般的链接 - 同在,与QUOT;若(1 LT; A&LT; = 5)QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆