assigning expession to variable

A

ambika

Hello,
Hello,
When the below pgm is executed The output is "1"..How is that?How does
"i" get that value "1"..Only one condition is true here(x<y)but z is
not greater than x or y,then how does it work?
Thanks to all those who are gonna respond.
--ambika


#include<stdio.h>
int main()
{
int x=10,y=20,z=5,i;
i=x<y<z;
printf("\n%d",i);
return(0);
}

Output:

1
 
P

Pieter Droogendijk

On 3 Sep 2003 02:31:32 -0700
Hello,
Hello,
When the below pgm is executed The output is "1"..How is that?How does
"i" get that value "1"..Only one condition is true here(x<y)but z is
not greater than x or y,then how does it work?
Thanks to all those who are gonna respond.
--ambika


#include<stdio.h>
int main()
{
int x=10,y=20,z=5,i;
i=x<y<z;
printf("\n%d",i);
return(0);
}

Output:

1

The < operator is evaluated left to right, and 10<20 is true, and true is
represented by 1.
Stare at this for a while:
i = x<y<z
i = (x<y)<z
i = (10<20)<5
i = 1<5
i = 1
 
T

Thomas Matthews

Pieter said:
On 3 Sep 2003 02:31:32 -0700
(e-mail address removed) (ambika) wrote: [snip]


The < operator is evaluated left to right, and 10<20 is true, and true is
represented by 1.
Stare at this for a while:
i = x<y<z
i = (x<y)<z
i = (10<20)<5
i = 1<5
i = 1

My understanding is that a boolean expression is converted to
zero or nonzero (i.e. any value).

Is the conversion actually to a value of 1 (one) for a
boolean expression that is true?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
E

Eric Sosman

Thomas said:
My understanding is that a boolean expression is converted to
zero or nonzero (i.e. any value).

Not quite. A Boolean *test* treats zero as "false" and
non-zero as "true," but a Boolean *operator* always produces
either zero or one, never 42.
 
K

Kevin Easton

Thomas Matthews said:
My understanding is that a boolean expression is converted to
zero or nonzero (i.e. any value).

Is the conversion actually to a value of 1 (one) for a
boolean expression that is true?

The result of a relational operator is 1 for true and 0 for false.

- Kevin.
 
J

Jack Klein

Pieter said:
On 3 Sep 2003 02:31:32 -0700
(e-mail address removed) (ambika) wrote: [snip]


The < operator is evaluated left to right, and 10<20 is true, and true is
represented by 1.
Stare at this for a while:
i = x<y<z
i = (x<y)<z
i = (10<20)<5
i = 1<5
i = 1

My understanding is that a boolean expression is converted to
zero or nonzero (i.e. any value).

Is the conversion actually to a value of 1 (one) for a
boolean expression that is true?

Others have given you a partial reply, correct as far as they went.

The "as-if" rule allows a compiler to determine truth or falsehood of
a Boolean expression and branch accordingly without generating a value
at all.

But if you actually use the result of a Boolean expression by value,
such as using it in a calculation or assigning it to an object of
arithmetic type, then it must produce exactly 0 or 1.

Comes in handy for doing things like outputting a string representing
a value in binary, as per partial snippet:

putchar('0' + !!(val & mask));

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top