if(a>b>c)

Discussion in 'C Programming' started by Adee, Feb 16, 2013.

  1. Adee

    Adee Guest

    Problem is :
    let int a=5,b=16,c=7;

    if(a<b<c)
    {
    printf("C is greater %d", c);
    }

    in above case still this statement executed. Any one explain the reason !
    Adee, Feb 16, 2013
    #1
    1. Advertising

  2. Adee

    Lew Pitcher Guest

    On Saturday 16 February 2013 03:14, in comp.lang.c,
    wrote:

    > Problem is :
    > let int a=5,b=16,c=7;
    >
    > if(a<b<c)
    > {
    > printf("C is greater %d", c);
    > }
    >
    > in above case still this statement executed. Any one explain the reason !


    Short answer: the "condition" part of an if() statement doesn't work the way
    you think it works. Go back to your tutorial, and look up "Relational
    Operators".

    --
    Lew Pitcher
    "In Skills, We Trust"
    Lew Pitcher, Feb 16, 2013
    #2
    1. Advertising

  3. Adee

    BartC Guest

    "Adee" <> wrote in message
    news:...
    > Problem is :
    > let int a=5,b=16,c=7;
    >
    > if(a<b<c)
    > {
    > printf("C is greater %d", c);
    > }
    >
    > in above case still this statement executed. Any one explain the reason !


    Because (a<b<c) doesn't mean (a<b && b<c), if that's what you assumed.

    It means ((a<b)<c), where a<b will be 0 or 1 (1 in this case).

    --
    Bartc
    BartC, Feb 16, 2013
    #3
  4. Adee

    John Bode Guest

    On Saturday, February 16, 2013 2:14:10 AM UTC-6, Adee wrote:
    > Problem is :
    >
    > let int a=5,b=16,c=7;
    >
    > if(a<b<c)
    > {
    > printf("C is greater %d", c);
    > }
    >
    > in above case still this statement executed. Any one explain the reason !


    "a<b<c" doesn't work the way you are expecting it to.

    From the C language standard (2011 version, n1570):

    > 6.5.8 Relational Operators
    > Syntax
    > 1 relational-expression:
    > shift-expression
    > relational-expression < shift-expression
    > relational-expression > shift-expression
    > relational-expression <= shift-expression
    > relational-expression >= shift-expression
    > ...
    > 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 speciï¬ed relation is true and 0 if it is false.107) The result
    > has type int.
    > ___________
    >
    > 107) The expression a<b<c is not interpreted as in ordinary mathematics.
    > As the syntax indicates, it means (a<b)<c; in other words, ‘‘if a is less
    > than b, compare 1 to c; otherwise, compare 0 to c’’.


    In this case, the result of a<b is always going to be less than c, whether
    a<b is true or not, so the result of the expression a<b<c will always be 1.
    John Bode, Feb 21, 2013
    #4
    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.

Share This Page