11

由于任何非零都表示为真,但>,<==运算符返回1为真,我很好奇是否有任何值得注意的 C 编译器,其中这些运算符可以产生大于1.

换句话说,是否有任何编译器 int i = (a==b); i如果我不打算将其用作布尔值,而是用作整数,并且假设它是0or ,则会导致未定义的1行为

4

7 回答 7

19

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


C 将整数值解释为布尔值的规则规定0为假,任何其他值都为真。请参阅处理 的 C11 部分if/while/do/for,它们都包含类似"while the expression compares unequal to zero". 具体来说:

6.8.4.1/2: In both forms [of the if statement, one with and one without an else clause], the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0.

6.8.5/4: An iteration statement [while, do and for] causes a statement called the loop body to be executed repeatedly until the controlling expression compares equal to 0.


但是,对于比较类型的表达式,您将得到什么结果是非常明确的,您要么0得到1. 这些C11标准的相关位都在6.5 Expressions

6.5.8/6: 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.

6.5.9/3: The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1 if the specified relation is true and 0 if it is false.

6.5.13/3: The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0.

6.5.14/3: The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0.

6.5.3.3/5: The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0.


这种行为可以追溯到C99 和 C89(ANSI 时代)。处理关系和相等运算符的 C99 部分还声明返回值为 0 或 1。

而且,虽然 C89 草案没有明确规定相等运算符的返回值,但它确实说:

==(等于)和 !=(不等于)运算符类似于关系运算符,只是它们的优先级较低。

关系运算符部分确实指出:

如果指定的关系为真,则每个运算符 <(小于)、>(大于)、<=(小于或等于)和 >=(大于或等于)应产生 1,如果是,则应产生 0错误的。

参考: http: //flash-gordon.me.uk/ansi.c.txt因为我没有任何 C89 标准的副本。我确实有 K&R 的第二版(1988 年的 ANSI 版本),在参考手册附录 A 的 A7.9 和 A7.10 部分中基本上说了同样的话。如果您想从第一版中得到明确的答案,那必须来自一个妻子不太容易扔掉旧垃圾的人。


附录:

根据迈克尔·伯尔(Michael Burr)的说法,在保存旧书方面,他要么没有结婚,要么有一个比我更乐于助人的妻子 :-)

K&R 1st Edition (1978) 在 7.6 和 7.7 中也有同样的说法:“如果指定的关系为假,则 [关系运算符] 都产生 0,如果为真,则产生 1。” ...“[等式运算符] 与关系运算符完全相同,只是它们的优先级较低。”

于 2012-05-17T08:33:15.527 回答
9

他们保证返回01

参考:
C99 标准:6.5.9 等式运算符

第 3 段:

==(等于)和!=(不等于)运算符类似于关系运算符,除了它们的优先级较低。108)如果指定的关系为真,则每个运算符产生 1,如果为假,则返回 0。结果的类型为 int。对于任何一对操作数,其中一个关系是正确的。

于 2012-05-17T08:34:48.283 回答
4

I don't think people here are answering the question. Question was not about what the standard says, the question was about if there are any notable compilers out there that do not obey the standard in that regard.

No, as far as I know, there are not.

于 2012-05-17T09:01:35.010 回答
3

我无权访问 C 标准,但根据Wikipedia

C 使用整数类型,其中 i > j 等关系表达式和由 && 和 || 连接的逻辑表达式 被定义为如果为真则值为 1,如果为假则为 0,而 if、while、for 等的测试部分将任何非零值视为真。

幸运的是,维基百科确实有正确的引用,但由于它们是对书籍的引用,因此不确定这有多大帮助:)。

于 2012-05-17T08:34:13.413 回答
3

据我所知,这是来自C99 标准的6.5.93段

==(等于)和!=(不等于)运算符类似于关系运算符,除了它们的优先级较低。108)如果指定的关系为真,则每个运算符产生 1,如果为假,则返回 0。结果的类型为 int。对于任何一对操作数,其中一个关系是正确的。

因此,评估值似乎应该是 1 或 0

于 2012-05-17T08:34:59.030 回答
3

按照规范,要被视为 C 语言,条件运算符必须返回 0 表示假,1 表示真。

俳句形式:

Specification
Disallows this behavior,
Otherwise not C.
于 2012-05-17T08:35:56.803 回答
2

在 c89/c90/ANSI-C 之前,比较运算符保证在“假”条件下产生零值,否则不等于零。c89 引入的真正“标准化”的替代品 1a = (b==c) ? 1 : 0;很少需要,如果需要,可以使用.

于 2012-05-17T08:49:03.553 回答