任何C编译器在哪里QUOT&; =="计算结果为大于一? [英] Any C compiler where "==" evaluates to larger than one?

查看:59
本文介绍了任何C编译器在哪里QUOT&; =="计算结果为大于一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为任何非零意味着真实的,但<​​code>&GT; ,&LT; == 等运营商返回 1 真正的,我很好奇,如果有任何显着的C编译器,而这些运营商可以导致价值大于 1

As anything non-zero means true, but the >, <, == etc. operators returning 1 for true, I'm curious if there are any notable C compilers where these operators can result in a value greater than 1.

在换句话说,有没有编译其中int I =(A == B);会导致不确定的行为,如果我打算用 I 不是一个布尔值,但作为一个整数,并假设它是不是 0 1

In other words, is there any compiler where int i = (a==b); would result in undefined behavior if I intended to use i not as a boolean value, but as an integer, and was assuming it would be either 0 or 1 ?

推荐答案

没有,如果有,他们不是C编译器的:-)关系和相等运算符返回1代表真,0为假的。该标准要求

No, if there are, they're not C compilers :-) The standard mandates that relational and equality operators return 1 for true and 0 for false.

有关的间pretation 的积分值由13 C布尔值的规则规定 0 为假,任何其他值为true 。见C11部分处理如果/当/ DO / FOR ,其中都含有类似而前pression比较不等于零的语言。具体做法是:

The rule for interpretation of integral values as boolean values by C states that 0 is false and any other value is true. See C11 sections dealing with if/while/do/for, which all contain language like "while the expression compares unequal to zero". Specifically:

6.8.4.1/2:在[if语句,其中一个的,一个没有else子句]这两种形式,如果EX pression比较不等于0则执行第一个子在其他的形式中,如果EX pression比较等于0时执行第二子语句。

6.8.5 / 4:循环语句[一会儿,做和]引起声明呼吁循环体被重复执行,直到控制前pression比较等于0

然而,这是相当明确的,你会得到什么结果进行比较式前pressions,你要么 0 1 。对于这些C11标准都是根据相关位 6.5防爆pressions


However, it's quite explicit what result you will get for the comparison-type expressions, you either get 0 or 1. The relevant bits of the C11 standard for these are all under 6.5 Expressions:

6.5.8 / 6:每个运营商和LT的; (小于),GT; (大于),LT =(小于或等于),和方式&gt; =(大于或等于)应得到1-如果指定的关系为真和0,如果它是假

6.5.9 / 3:该==(等于)和=(不等于)运算符类似于除了其较低的precedence关系运算符。每个运营商产生1,如果指定的关系是真,0如果是假的。

6.5.13 / 3:&功放;&安培;经营者应当产生1,如果两个操作数的比较不等于0;否则,它产生0。

6.5.14 / 3:||经营者应当产生1如果任一操作数的比较不等于0;否则,它产生0。

6.5.3.3/5:逻辑否定运算符的结果!是0,如果其操作数的值进行比较,不等于0,1,如果它的操作数的值进行比较,等于0。

和这种行为去的办法的回C99 C89和(ANSI天)。 C99的部分处理关系和平等的运营商也指出,返回值是0或1。


And this behaviour goes way back to C99 and C89 (ANSI days). The C99 sections dealing with relational and equality operators also states that the return values is either 0 or 1.

和,而不会在C89草案的明确的决定平等运营商的返回值,它会说:

And, while the C89 draft doesn't explicitly dictate the return values for equality operators, it does say:

的==(等于)和!=(不等于)运算类似于除了它们的较低precedence关系运算

The == (equal to) and the != (not equal to) operators are analogous to the relational operators except for their lower precedence.

和关系运算部分的确实的状态:

And the relational operators section does state:

每个运营商和LT的; (小于),>(大于),LT; =(小于或等于),和> =(大于或等于)应得到1-如果指定的关系为真和0,如果它是假

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.

参考: http://flash-gordon.me.uk/ansi.c.txt 因为我没有的C89标准的左右浮动任何副本。我的的有K&放大器的第二版; R(在ANSI一名来自1988年),它基本上是说同样的事情,并在部分A7.9附录A的参考手册A7.10。如果你想从第一版一个明确的答案,那将不得不来自某人不容易抛出旧垃圾妻子。

Reference: http://flash-gordon.me.uk/ansi.c.txt since I don't have any copies of the C89 standards floating around. I do have the second edition of K&R (the ANSI one from 1988) which basically says the same thing, in sections A7.9 and A7.10 of Appendix A, the Reference Manual. If you want a definitive answer from the first edition, that'll have to come from someone with a wife less prone to throwing out old junk.

附录:

据迈克尔·伯尔,谁是要么不结婚,或在保持旧书方面有着更为包容老婆比我: - )

According to Michael Burr, who is either not married or has a more accommodating wife than I in terms of keeping old books :-)

K&安培; R第1版(1978年)也说,在7.6和7.7是相同的:在[关系运算符],都产生0,如果指定的关系是假,1如果这是真的。 ......在[等式运算符]是完全类似,除了其较低的precedence的关系运算符。

K&R 1st Edition (1978) also says the same in 7.6 and 7.7: "The [relational operators] all yield 0 if the specified relation is false and 1 if it is true." ... "The [equality operators] are exactly analogous to the relational operators except for their lower precedence."

这篇关于任何C编译器在哪里QUOT&; ==&QUOT;计算结果为大于一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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