if(a,b,c)

Discussion in 'C Programming' started by hyderabadblues, Oct 11, 2005.

  1. Why the following statement evaluates as false infact it doesn't follow
    the syntax

    int a=10,b=8,c=0;

    if(a,b,c)
    printf("=");
    else
    printf("!=");

    prints !=
    hyderabadblues, Oct 11, 2005
    #1
    1. Advertising

  2. hyderabadblues wrote:
    >
    > Why the following statement evaluates as false infact it doesn't follow
    > the syntax
    >
    > int a=10,b=8,c=0;
    >
    > if(a,b,c)
    > printf("=");
    > else
    > printf("!=");
    >
    > prints !=


    Because c==0, therefore (a,b,c)==0, therefore "if (a,b,c)" is false.

    And what syntax does it not follow?

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Oct 11, 2005
    #2
    1. Advertising

  3. hyderabadblues

    Eric Sosman Guest

    hyderabadblues wrote On 10/11/05 13:54,:
    > Why the following statement evaluates as false infact it doesn't follow
    > the syntax
    >
    > int a=10,b=8,c=0;
    >
    > if(a,b,c)
    > printf("=");
    > else
    > printf("!=");
    >
    > prints !=


    Search your C textbook for "the comma operator."

    --
    Eric Sosman, Oct 11, 2005
    #3
  4. hyderabadblues wrote:
    > Why the following statement evaluates as false infact it doesn't follow
    > the syntax


    What do you mean "it doesn't follow the syntax"?
    >
    > int a=10,b=8,c=0;
    >
    > if(a,b,c)


    The commas are comma operators (what did you think they were?), so the
    above is just
    if (c)
    with the side effect of evaluating a and b along the way.
    Since (c == 0), the condition is never true, so ...

    > printf("=");
    > else
    > printf("!=");
    >
    > prints !=


    of course.

    And next time please post compilable code.
    >
    Martin Ambuhl, Oct 11, 2005
    #4
  5. "hyderabadblues" <> wrote in message
    news:...
    > Why the following statement evaluates as false infact it doesn't follow
    > the syntax
    >
    > int a=10,b=8,c=0;
    >
    > if(a,b,c)
    > printf("=");
    > else
    > printf("!=");
    >
    > prints !=
    >


    IIRC, the comma operator results in the evaluation of all arguments, then
    returns the value of the LAST (i.e. rightmost) argument. In this case, it
    evaluates the expressions "a", "b", and "c" and then returns the value of
    the last expression ("c"), which is 0. Thus, the correct behavior is for
    the "if" statement to be false, and the "else" block to be executed, as you
    observed. What were you expecting it to do?

    -Charles
    Charles M. Reinke, Oct 11, 2005
    #5
  6. hyderabadblues

    Default User Guest

    hyderabadblues wrote:

    > Why the following statement evaluates as false infact it doesn't
    > follow the syntax


    Yes it does.

    > int a=10,b=8,c=0;
    >
    > if(a,b,c)


    Look in your book to see how the comma operator works.



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Oct 11, 2005
    #6
  7. hyderabadblues

    Mike Wahler Guest

    "hyderabadblues" <> wrote in message
    news:...
    > Why the following statement evaluates as false


    Because 'c' evaluates to zero, and zero always
    evaluates to false (non-zero always evaluates to
    true).

    > infact it doesn't follow
    > the syntax


    Sure it does. It's valid C syntax anyway.
    You just don't appear to understand what
    that syntax does.

    Get a C book (or more).

    -Mike

    >
    > int a=10,b=8,c=0;
    >
    > if(a,b,c)
    > printf("=");
    > else
    > printf("!=");
    >
    > prints !=
    >
    Mike Wahler, Oct 11, 2005
    #7
  8. hyderabadblues

    Guest

    hyderabadblues wrote:
    > Why the following statement evaluates as false infact it doesn't follow
    > the syntax
    >
    > int a=10,b=8,c=0;
    >
    > if(a,b,c)
    > printf("=");
    > else
    > printf("!=");
    >
    > prints !=


    The syntax is fine and the behaviour of the program as per the
    semantics of "," operator. It's associativity is left-to-right and if a
    pair of expressions seperated by a comma is evaluated left-to-right.
    So, there's no ambiguity even with the behaviour as well. So, the
    program always prints !=.

    Regards,
    Raju
    , Oct 12, 2005
    #8
    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