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

assigning expession to variable

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 3 Sep 2003 02:31:32 -0700
in*******@yahoo.com (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#;+,)/'///*");}
Nov 13 '05 #2

P: n/a
Ema
"Pieter Droogendijk" <gi*@binky.homeunix.org> ha scritto nel messaggio
news:20030903113833.439b97e8.gi*@binky.homeunix.or g...
On 3 Sep 2003 02:31:32 -0700
in*******@yahoo.com (ambika) wrote:
Hello,

[...]


and the right assignment should be:

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

Bye,
Ema
Nov 13 '05 #3

P: n/a
Pieter Droogendijk wrote:
On 3 Sep 2003 02:31:32 -0700
in*******@yahoo.com (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

Nov 13 '05 #4

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

--
Er*********@sun.com
Nov 13 '05 #5

P: n/a
Thomas Matthews <Th**********************@sbcglobal.net> 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.

Nov 13 '05 #6

P: n/a
On Wed, 03 Sep 2003 14:33:28 GMT, Thomas Matthews
<Th**********************@sbcglobal.net> wrote in comp.lang.c:
Pieter Droogendijk wrote:
On 3 Sep 2003 02:31:32 -0700
in*******@yahoo.com (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
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.