By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Dec 28 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 27 Dec 2005 20:21:06 -0800, "moosdau" <mo*****@gmail.com> 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>>
Dec 28 '05 #2

P: n/a
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.:)

Dec 28 '05 #3

P: n/a
Barry Schwarz wrote:
On 27 Dec 2005 20:21:06 -0800, "moosdau" <mo*****@gmail.com> 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
Dec 28 '05 #4

P: n/a

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.

Dec 28 '05 #5

P: n/a
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.

Dec 28 '05 #6

P: n/a
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.
--
Thad
Dec 29 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.