Order of evaluation -- clash w/sequentiel expression operator

Discussion in 'C Programming' started by Jan Engelhardt, Aug 20, 2003.

  1. Hi,


    I was told that order of evaluation is unspecified for functions, i.e.

    int f = 0;
    print_results(modify(&f), modify(&f), modify(&f));

    where i.e. modify() increases f by one. In my case w/gcc, it was evaluated from
    right-to-left (gcc does a nice stack optimization). Not what I expected though.
    Anyway:

    Taking a look at the C operator precedence table, I find

    , sequential expression left-to-right

    at the bottom. So what's wrong here?

    --
    - Jan Engelhardt
    Jan Engelhardt, Aug 20, 2003
    #1
    1. Advertising

  2. Jan Engelhardt

    Ian Woods Guest

    Jan Engelhardt <> wrote in
    news:p:

    > Hi,
    >
    >
    > I was told that order of evaluation is unspecified for functions, i.e.
    >
    > int f = 0;
    > print_results(modify(&f), modify(&f), modify(&f));
    >
    > where i.e. modify() increases f by one. In my case w/gcc, it was
    > evaluated from right-to-left (gcc does a nice stack optimization). Not
    > what I expected though. Anyway:
    >
    > Taking a look at the C operator precedence table, I find
    >
    > , sequential expression left-to-right
    >
    > at the bottom. So what's wrong here?


    From memory, the comma as an operator and a comma in a function invokation
    are different things entirely. The order of evaluation of function
    arguments is undefined.

    Ian Woods
    Ian Woods, Aug 20, 2003
    #2
    1. Advertising

  3. In article <>,
    Jan Engelhardt <> wrote:

    > Hi,
    >
    >
    > I was told that order of evaluation is unspecified for functions, i.e.
    >
    > int f = 0;
    > print_results(modify(&f), modify(&f), modify(&f));
    >
    > where i.e. modify() increases f by one. In my case w/gcc, it was evaluated
    > from
    > right-to-left (gcc does a nice stack optimization). Not what I expected
    > though.
    > Anyway:
    >
    > Taking a look at the C operator precedence table, I find
    >
    > , sequential expression left-to-right
    >
    > at the bottom. So what's wrong here?



    You are confusing "comma expression" and function arguments. If you look
    at the syntax of a function call you will find that the arguments are
    not "expression"s but "assignment-expression"s. For example, the
    comma-exppression x,y,z is _not_ an assignment expression, so f(x,y,z)
    has three arguments, and not one.
    Christian Bau, Aug 20, 2003
    #3
  4. Jan Engelhardt

    Mike Wahler Guest

    Jan Engelhardt <> wrote in message
    news:p...
    > Hi,
    >
    >
    > I was told that order of evaluation is unspecified for functions,


    For function arguments.

    > i.e.
    >
    > int f = 0;
    > print_results(modify(&f), modify(&f), modify(&f));
    >
    > where i.e. modify() increases f by one. In my case w/gcc, it was evaluated

    from
    > right-to-left (gcc does a nice stack optimization). Not what I expected

    though.

    Since the language does not specify order of
    evaluation, any expectation about it is spurious.

    > Anyway:
    >
    > Taking a look at the C operator precedence table, I find



    >
    > , sequential expression left-to-right


    Where did you encounter the term 'sequential expression'?
    I don't find it in my copy of the C standard or in K&R.
    Also, which operators are you referring to?

    >
    > at the bottom. So what's wrong here?


    What's wrong with what?

    Order of evaluation and operator precedence are two
    separate, distinct concepts. Given:


    f(x) + g(y) * h(z);

    Since order of evaluation is not specified,
    we cannot know which of 'x', 'y' or 'z' is
    evaluated first.

    But precedence rules do specify that the
    return values of 'g()' and 'h()' are multiplied
    first (but we don't know which of 'g()' or 'h()'
    is invoked first), then the return value of 'f()' is added
    to that product.

    Does that clarify things at all?

    -Mike
    Mike Wahler, Aug 20, 2003
    #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.
Similar Threads
  1. Christos TZOTZIOY Georgiou

    Introspection: expression evaluation order - precedence

    Christos TZOTZIOY Georgiou, Oct 24, 2003, in forum: Python
    Replies:
    0
    Views:
    299
    Christos TZOTZIOY Georgiou
    Oct 24, 2003
  2. Frank Wallingford

    Sub-sub-expression evaluation order

    Frank Wallingford, Dec 9, 2004, in forum: C Programming
    Replies:
    4
    Views:
    343
    Chris Torek
    Dec 10, 2004
  3. Replies:
    26
    Views:
    660
    Mark McIntyre
    Jun 15, 2007
  4. Replies:
    2
    Views:
    285
    Andrew Koenig
    Jul 7, 2007
  5. Stasa Medjedovic

    Order of expression evaluation

    Stasa Medjedovic, May 15, 2008, in forum: C++
    Replies:
    6
    Views:
    449
    Kai-Uwe Bux
    May 16, 2008
Loading...

Share This Page