3

Does the C standard ensure that the boolean operations (==, !=, >, &&, ||, etc) always have the same value to represent truthfulness? In other words, do they always return some positive constant if true, or is the only guarantee that it will be a positive number?

The reason I'm asking this is to know if statements such as the one below are valid. That expression should be true if both pointers are NULL or both pointers point somewhere (not necessarily the same place).

if ((ptr1 == NULL) == (ptr2 == NULL)) 
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
  • Will this statement invoke short circuit evaluation? Anyway, wouldn't the statement be better expressed as "if (((ptr1 == NULL) && (ptr2 == NULL)) || ((ptr1 != NULL) && (ptr2 != NULL)))" ? For me, that makes the intent of the statement a bit clearer. – Rob Wells May 08 '12 at 10:21
  • Sorry @Paul Manta I hit the send key before finishing my thought! :-( – Rob Wells May 08 '12 at 10:26
  • @RobWells It's debatable which version is clearer. The one I asked about is a lot shorter at any rate, and I didn't get anyone asking me what it's supposed to mean so I guess there's no real reason not to use it. It's in the same style as doing `(a * b) > 0` to find out if two numbers are both negative or both positive. – Paul Manta May 08 '12 at 10:36
  • but we don't know how long it took them to understand the full implication of your original version. My version makes the intent clear immediately without spending extra time having to puzzling over it. As Martin Fowler said, "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." :-) BTW I like your test for both positive or both negative! – Rob Wells May 08 '12 at 10:37

3 Answers3

7

Yes, although the rule for interpretation of integral values as boolean values by C states that 0 is false and any other value is true (a), the results of comparison operators is always 1 or 0.

So the expression (a == b) will never give you 42, for example.

The relevant bits of the standard (C11) 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.

That covers all the ones you explicitly mentioned in your question. The only other boolean operation I can think of (off the top of my head) is the logical NOT operator ! which is also covered:

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.


(a): See C11 sections dealing with if, while, do and for, which all contain language along the lines of something happen if/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.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

Yes.

Values generated by operators will always be 1 for true and 0 for false.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

The results of

  • negation operator (!)

  • relational operators (<, >, <=, >=)

  • equality operators (==, !=)

    logical operators (&&,  ||)

is an int value of either 0 (false) or 1 (true).

Community
  • 1
  • 1
ouah
  • 142,963
  • 15
  • 272
  • 331