# does the logical expression return 1 ( if it is true ) absolutely?

Discussion in 'C Programming' started by moosdau, Dec 28, 2005.

1. ### moosdauGuest

it's likely that the compiler could only promise returning a non-zero
value in my impression,
but it did return 1 every time,
like the code below:
int a=3,b=8;
printf("%d\n",a<b);

will it return 1 absolutely in any case?

moosdau, Dec 28, 2005

2. ### Barry SchwarzGuest

On 27 Dec 2005 20:21:06 -0800, "moosdau" <> wrote:

>it's likely that the compiler could only promise returning a non-zero
>value in my impression,
>but it did return 1 every time,
>like the code below:
>int a=3,b=8;
>printf("%d\n",a<b);
>
>will it return 1 absolutely in any case?

Yes. To quote from K&R: "By definition, the numeric value of a
relation or logical expression is 1 if the relation is true and 0 if
the relation is false." I really doubt the new standard would change
this since a lot of code depends on it.

<<Remove the del for email>>

Barry Schwarz, Dec 28, 2005

3. ### moosdauGuest

thanks very much!
I didn't read K&R 's book when I learn c.
the book I read only refer that the value is non zero,didn't mention it
must be 1.
now I know.

moosdau, Dec 28, 2005
4. ### AnandGuest

Barry Schwarz wrote:
> On 27 Dec 2005 20:21:06 -0800, "moosdau" <> wrote:
>
>
>>it's likely that the compiler could only promise returning a non-zero
>>value in my impression,
>>but it did return 1 every time,
>>like the code below:
>>int a=3,b=8;
>>printf("%d\n",a<b);
>>
>>will it return 1 absolutely in any case?

>
>
> Yes. To quote from K&R: "By definition, the numeric value of a
> relation or logical expression is 1 if the relation is true and 0 if
> the relation is false." I really doubt the new standard would change
> this since a lot of code depends on it.
>

You are right, there's no change in this aspect, and C99 also defines it
as 1 and 0.
To quote from std:
+----C99:6.5.8 Relational operators-----
|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.
| The result has type int.
+---------------------------------------

and 6.5.9 (Equality Operators) goes on to define the == and != as above.
--
(Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
(clc FAQ) http://c-faq.com

Anand, Dec 28, 2005
5. ### John BodeGuest

moosdau wrote:
> it's likely that the compiler could only promise returning a non-zero
> value in my impression,
> but it did return 1 every time,
> like the code below:
> int a=3,b=8;
> printf("%d\n",a<b);
>
> will it return 1 absolutely in any case?

Yes. The result of a relational (<, <=, >, >=) or equality (==, !=)
expression is always either 1 or 0, and is always of type int.

It is also true that any non-zero integral expression in a Boolean
context will evaluate to true, so given an int variable i=123,

if (i != 0) {/* do stuff */}

and

if (i) {/* do stuff */}

are equivalent.

John Bode, Dec 28, 2005
6. ### tmp123Guest

It is a usual confusion: one thing is that the "if" statement goes to
the "then" part for any value different of 0 and another one is what is
returned by C operators.

tmp123, Dec 28, 2005
7. ### Thad SmithGuest

moosdau wrote:
> thanks very much!
> I didn't read K&R 's book when I learn c.
> the book I read only refer that the value is non zero,didn't mention it
> must be 1.
> now I know.

What confuses some people is that while equality and relational
operators evaluate to 0 and 1, the operands of if(), while(), ?:, ||,
&&, !, and middle operand of for(; are compared with 0, so any
non-zero value will cause the same result of the test.

--