Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any C compiler where "==" evaluates to larger than one?

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.

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 ?

like image 607
vsz Avatar asked May 17 '12 08:05

vsz


2 Answers

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.


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: 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.


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: 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.


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.

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

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:

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.

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.


Addendum:

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 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."

like image 155
paxdiablo Avatar answered Oct 26 '22 23:10

paxdiablo


They are guaranteed to return 0 or 1.

Reference:
C99 Standard: 6.5.9 Equality operators

Para 3:

The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.108) Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.

like image 38
Alok Save Avatar answered Oct 27 '22 00:10

Alok Save