sequential-evaluation operation result

Discussion in 'C Programming' started by Shivanand Kadwadkar, Jan 1, 2011.

  1. I far i know sequential-evaluation operator are evaluated from left
    and right and it will only take the right most result

    #include<stdio.h>
    int main()
    {
    int i=0,j=2,k=5;
    int ans1,ans2;

    ans1=(i,j,k); // Evaluated from left to right and we assign 5 to ans1
    ans2=i,j,k; // Why it is not same here that we assign 5 to ans2
    printf("ans1 %d ans2 %d \n",ans1,ans2);

    return 0
    }
    Output:
    ans1 5 ans2 0
    -------------------------------------------------

    My doubt is with ans2, is comma treated as separator here and we
    discard the right value and take only the left one
    Shivanand Kadwadkar, Jan 1, 2011
    #1
    1. Advertising

  2. Shivanand Kadwadkar

    Ike Naar Guest

    On 2011-01-01, Shivanand Kadwadkar <> wrote:
    > int i=0,j=2,k=5;
    > int ans1,ans2;
    >
    > ans1=(i,j,k); // Evaluated from left to right and we assign 5 to ans1
    > ans2=i,j,k; // Why it is not same here that we assign 5 to ans2


    ``='' binds stronger than ``,'' so the last expression is parsed as

    (ans2=i),j,k;
    Ike Naar, Jan 1, 2011
    #2
    1. Advertising

  3. Shivanand Kadwadkar

    Seebs Guest

    On 2011-01-01, Shivanand Kadwadkar <> wrote:
    > I far i know sequential-evaluation operator are evaluated from left
    > and right and it will only take the right most result


    No, you don't. You just said something totally incoherent.

    > ans1=(i,j,k); // Evaluated from left to right and we assign 5 to ans1


    There is no left-to-right or right-to-left.

    Evaluation is not the same thing as *grouping*. What is relevant here
    is that you have an assignment of
    ans1
    =
    (i,j,k)

    > ans2=i,j,k; // Why it is not same here that we assign 5 to ans2


    Because here, you have
    ans1=i
    j
    k

    Which is to say, commas have lower precedence than equals signs; the
    expressions separated by commas can be assignment expressions including
    equals signs, so instead of assigning the value "i,j,k" to ans1, you're
    assigning the value "i" for your first expression, then j, then k.

    What you may be missing is that in C, assignment is just another kind
    of expression, not a "statement" or something.

    > My doubt is with ans2, is comma treated as separator here and we
    > discard the right value and take only the left one


    The word "doubt" is not correct here in standard English. A "doubt"
    is where someone told you something and you don't believe them.

    Anyway, C generally doesn't specify order of evaluation. Comma operators
    are special in that they do, but "order of evaluation" is totally
    distinct from "grouping".

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Jan 1, 2011
    #3
    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. Ilias Lazaridis
    Replies:
    2
    Views:
    388
    Ilias Lazaridis
    Apr 24, 2005
  2. david ullua
    Replies:
    13
    Views:
    663
  3. Ilias Lazaridis
    Replies:
    74
    Views:
    748
    Ilias Lazaridis
    Apr 4, 2005
  4. Ilias Lazaridis
    Replies:
    18
    Views:
    330
    Bill Guindon
    Apr 9, 2005
  5. Michael Tan
    Replies:
    32
    Views:
    960
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page