assigning expession to variable

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

  1. ambika

    ambika Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. ambika

    Ema Guest

    "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
    #3
  4. 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
    #4
  5. ambika

    Eric Sosman Guest

    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
    #5
  6. ambika

    Kevin Easton Guest

    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
    #6
  7. ambika

    Jack Klein Guest

    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:

    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
     
    Jack Klein, Sep 4, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Cox [Microsoft MVP]

    Re: Assigning contents within HTML tag to string variable.

    Ken Cox [Microsoft MVP], Aug 5, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    901
    tonymjohn
    Aug 7, 2004
  2. RA
    Replies:
    4
    Views:
    4,665
  3. Newbie
    Replies:
    6
    Views:
    8,523
    Bob Barrows [MVP]
    Jan 24, 2005
  4. DOTNET
    Replies:
    3
    Views:
    530
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Sep 14, 2005
  5. weston
    Replies:
    1
    Views:
    265
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page