# assigning expession to variable

Discussion in 'C Programming' started by ambika, Sep 3, 2003.

1. ### ambikaGuest

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

ambika, Sep 3, 2003

2. ### Pieter DroogendijkGuest

On 3 Sep 2003 02:31:32 -0700
(ambika) wrote:
> 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

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k)x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh\$%c","-34*1'.+(,03#;+,)/'///*");}

Pieter Droogendijk, Sep 3, 2003

3. ### EmaGuest

"Pieter Droogendijk" <> ha scritto nel messaggio
news:...
> On 3 Sep 2003 02:31:32 -0700
> (ambika) wrote:
> > Hello,

> [...]

and the right assignment should be:

i= (x<y) && (y<z);

Bye,
Ema

Ema, Sep 3, 2003
4. ### Thomas MatthewsGuest

Pieter Droogendijk wrote:

> On 3 Sep 2003 02:31:32 -0700
> (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

Thomas Matthews, Sep 3, 2003
5. ### Eric SosmanGuest

Thomas Matthews wrote:
>
> 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.

--

Eric Sosman, Sep 3, 2003
6. ### Kevin EastonGuest

Thomas Matthews <> wrote:
[...]
> 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.

Kevin Easton, Sep 3, 2003
7. ### Jack KleinGuest

On Wed, 03 Sep 2003 14:33:28 GMT, Thomas Matthews
<> wrote in comp.lang.c:

> Pieter Droogendijk wrote:
>
> > On 3 Sep 2003 02:31:32 -0700
> > (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:

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

Jack Klein, Sep 4, 2003