0

In section 2.6 "Relational and Logical Operators" on page 42 in "The C Programming Language" 2nd by K&R, the authors said:

By definition, the numeric value of a relational or logical expression is 1 if the relation is true, and 0 if the relation is false.

I'm confused by that wording. For example, in the logical expression

x && y

there is no relation which can be true or false. Isn't the wording supposed to be:

By definition, the numeric value of a relational or logical expression is 1 if the expression is true, and 0 if the expression is false.

  • They mean `a = (x == 42);` where `a` will be `0` or `1`. – Weather Vane Feb 02 '18 at 18:23
  • 1
    K&R is just a book. For more formal language see the C standard. – Eugene Sh. Feb 02 '18 at 18:26
  • IMO, "expression" matches the C spec better. So I agree with your supposed wording. See 6.5 Expressions – chux - Reinstate Monica Feb 02 '18 at 18:26
  • @Weather Vane Yes, that's correct. But then the definition should be "By definition, the numeric value of a relational expression is 1 if the relation is true, and 0 if the relation is false." –  Feb 02 '18 at 18:27
  • Just to clarify, you think the wording is wrong, that the word "relation" is wrong? Well, the book isn't the C specification, it's not an authoritative source even if the book was written by the inventors of C. – Some programmer dude Feb 02 '18 at 18:28
  • 4
    Apparently, you are not confused enough to not be able to understand and paraphrase it in you own words. So, the sentence does its job, it's good enough. Admittedly, I'd also prefer "expression". Wouldn't the whole discussion be better suited for https://english.stackexchange.com/ ? – Andrey Tyukin Feb 02 '18 at 18:29
  • When you write `x && y`, a comparison to zero is inserted implicitly for you, so the effect is the same as if you wrote `x != 0 && y != 0`. – Sergey Kalinichenko Feb 02 '18 at 19:40

4 Answers4

1
x && y

Since x and y are being used in conjuntion with && the compiler needs logical (boolean)values

In c 0 is false and not 0 is true. Hence the statement you posted from the standard that a logical expression evaluates to 0 or not zero (in fact 1, but in testing any non zero value is treated as true).

Thus this code is interpreted as

(x != 0) && (y != 0)

the depending of the current values of x and y this becomes, say

1 && 0

THis is

true && false

by the rule that 0 == false and !0 == true. This expression is thus false, and therefore evaluates to 0 by the statment you posted.

Note that much of this confusion comes form the fact the c originally had no boolean type. Hence the convention that 0 = false and !0 = true. This has the beneficial side effect of allowing numbers and in particular pointers to be tested directly as in

while(ptr--)
{
}

Which is equivalent to

while(ptr-- != 0)
{
}
pm100
  • 48,078
  • 23
  • 82
  • 145
0

This is not relational operator.(You shouldn't take the word relation on that context also). This is logical AND. The wording is correct.

Here each of the variable's value x and y if non zero will evaluate to truthy value. And if both of them are true then it will be true - the whole expression would be true.

And here by relation it means the relation of each subexpression being truthy or not which in turn decides whether whole expression is true or false. In your case the relation is simply whether x is nonzero or not AND y is nonzero or not.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • And what did I say? I haven't mentioned anything of a relational operator. –  Feb 02 '18 at 18:24
0

A relational or logical expression is the result of an operator such as &&, ||, ==, '>=,<=, etc. In this context,x && y` is a logical expression which will evaluate to either 0 or 1.

The arguments to && need not be logical expressions. When evaluated in a boolean context, a numeric value of 0 evaluates to false while a non-zero value evaluates to true. So in the case of x && y the expression will evaluate to 1 (i.e. true) if both x and y are non-zero.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

The wording is correct. Relation is a mathematical term.

Every operator in C returning a boolean can be considered a relation.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207