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

M

moosdau

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?
 
B

Barry Schwarz

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>>
 
M

moosdau

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

Anand

Barry said:
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.
 
J

John Bode

moosdau said:
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.
 
T

tmp123

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

Thad Smith

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top