Order of evaluation

Discussion in 'C Programming' started by int main(void), Oct 9, 2006.

  1. Hi all,

    In the following program,

    #include<stdio.h>
    int main(void)
    {
    int x = 10;
    int y = 10;

    x = x + (x = 20);
    y = y + y + (y = 20);

    printf("x = %d\ny=%d",x,y);
    return 0;
    }

    Output is :
    x = 40
    y = 40

    Why does the order of evaluation change when there are three operands
    ?


    Thanks for your time,
    Yugi.
     
    int main(void), Oct 9, 2006
    #1
    1. Advertising

  2. int main(void)

    Guest

    int main(void) wrote:

    > Hi all,
    >
    > In the following program,
    >
    > #include<stdio.h>
    > int main(void)
    > {
    > int x = 10;
    > int y = 10;
    >
    > x = x + (x = 20);
    > y = y + y + (y = 20);
    >
    > printf("x = %d\ny=%d",x,y);
    > return 0;
    > }
    >
    > Output is :
    > x = 40
    > y = 40


    No it isn't - you would have no spaces round the second "="...

    It doesn't do that on my AIX box, which uses a different compiler to
    the GNU C compiler I reproduced your result with on a Linux system.

    Here's my result:-

    x = 30
    y=40

    Someone more knowledgable than me will probably quote chapter and verse
    from the standard, but as you are altering a value used in a
    calculation, you're almost certainly invoking undefined behaviour .
     
    , Oct 9, 2006
    #2
    1. Advertising

  3. int main(void)

    Chris Dollin Guest

    int main(void) wrote:

    > Hi all,
    >
    > In the following program,
    >
    > #include<stdio.h>
    > int main(void)
    > {
    > int x = 10;
    > int y = 10;
    >
    > x = x + (x = 20);


    Undefined behaviour. Demons come out of your nose.

    > y = y + y + (y = 20);


    More undefined behaviour. The demons erase your memory
    of their presence, leaving only their droppings.

    > printf("x = %d\ny=%d",x,y);
    > return 0;
    > }
    >
    > Output is :
    > x = 40
    > y = 40
    >
    > Why does the order of evaluation change when there are three operands
    > ?


    (a) what makes you think there is /an/ order of evaluation?

    (b) when a single statement assigns values to the same variable,
    what makes you think this is well-defined in C?

    (c) It's explicitly undefined. Don't do that. Don't even try to
    /explain/ it: it's /undefined/. Anything can happen in the
    next half-hour.

    (d) To allow compilers latitude. (They already have longitude.)

    --
    Chris "tempest, but not in a teacup" Dollin
    "Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
     
    Chris Dollin, Oct 9, 2006
    #3
  4. wrote:

    > int main(void) wrote:
    >
    > > Hi all,
    > >
    > > In the following program,
    > >
    > > #include<stdio.h>
    > > int main(void)
    > > {
    > > int x = 10;
    > > int y = 10;
    > >
    > > x = x + (x = 20);
    > > y = y + y + (y = 20);
    > >
    > > printf("x = %d\ny=%d",x,y);
    > > return 0;
    > > }
    > >
    > > Output is :
    > > x = 40
    > > y = 40

    >
    > No it isn't - you would have no spaces round the second "="...
    >
    > It doesn't do that on my AIX box, which uses a different compiler to
    > the GNU C compiler I reproduced your result with on a Linux system.
    >
    > Here's my result:-
    >
    > x = 30
    > y=40
    >
    > Someone more knowledgable than me will probably quote chapter and verse
    > from the standard, but as you are altering a value used in a
    > calculation, you're almost certainly invoking undefined behaviour .


    Hi,


    I think that it has something to do with sequence points.
    I posted this question to understand sequence points.
    I read the FAQ on sequence points.
    But it is never clear to me no matter how much i read about it.
    I hope people in this group will help me.
    The problem i have is
    1) what is a sequence point ? Why is it defined that way ?
    2) Why is it needed ? What will happen if that restriction is not
    there ?
    3) Way to find whether any expression violates that rule .



    Thanks ,
    Yugi
     
    int main(void), Oct 9, 2006
    #4
  5. wrote:

    > int main(void) wrote:
    >
    > > Hi all,
    > >
    > > In the following program,
    > >
    > > #include<stdio.h>
    > > int main(void)
    > > {
    > > int x = 10;
    > > int y = 10;
    > >
    > > x = x + (x = 20);
    > > y = y + y + (y = 20);
    > >
    > > printf("x = %d\ny=%d",x,y);
    > > return 0;
    > > }
    > >
    > > Output is :
    > > x = 40
    > > y = 40

    >
    > No it isn't - you would have no spaces round the second "="...
    >
    > It doesn't do that on my AIX box, which uses a different compiler to
    > the GNU C compiler I reproduced your result with on a Linux system.
    >
    > Here's my result:-
    >
    > x = 30
    > y=40
    >
    > Someone more knowledgable than me will probably quote chapter and verse
    > from the standard, but as you are altering a value used in a
    > calculation, you're almost certainly invoking undefined behaviour .


    Hi,


    I think that it has something to do with sequence points.
    I posted this question to understand sequence points.
    I read the FAQ on sequence points.
    But it is never clear to me no matter how much i read about it.
    I hope people in this group will help me.
    The problem i have is
    1) what is a sequence point ? Why is it defined that way ?
    2) Why is it needed ? What will happen if that restriction is not
    there ?
    3) Way to find whether any expression violates that rule .



    Thanks ,
    Yugi
     
    int main(void), Oct 9, 2006
    #5
  6. In article <>,
    int main(void) <> wrote:

    >x = x + (x = 20);
    >y = y + y + (y = 20);


    > Why does the order of evaluation change when there are three operands?


    Because you're not allowed to do that, and the result is undefined.
    Don't change the value of a variable in an expression that uses it
    somewhere else.

    -- Richard
     
    Richard Tobin, Oct 9, 2006
    #6
  7. "int main(void)" <> writes:

    > But it is never clear to me no matter how much i read about it.
    > I hope people in this group will help me.
    > The problem i have is
    > 1) what is a sequence point ?


    A point where all side effect of expressions sequenced before are done and
    where no determination of side effet of expressions sequenced after are
    started.

    One important things is that the ordering is not total. A simple example,
    in

    f(g() && h(), i())

    there is a sequence point between evaluation of g() and h() but the
    evaluation of i() is not constrainted at all by it.

    > Why is it defined that way ?


    Because the standard tried also to describe the behaviour of compiler at
    the time where the standard was written.

    > 2) Why is it needed ?


    Performance is the usual justification.

    > What will happen if that restriction is not there ?


    The evaluation rules are more a liberty left to the implementor than
    restriction...

    > 3) Way to find whether any expression violates that rule .


    As a rule of tumb: in one expression, you can not read and write to the
    same object, excepted if the read is for determining the value written by
    the write.

    --
    Jean-Marc
     
    Jean-Marc Bourguet, Oct 9, 2006
    #7
  8. "int main(void)" <> wrote:

    > > Someone more knowledgable than me will probably quote chapter and verse
    > > from the standard, but as you are altering a value used in a
    > > calculation, you're almost certainly invoking undefined behaviour .


    From n869, 6.5 subparagraph 2:

    "Between the previous and next sequence point an object shall have its
    stored value modified at most once by the evaluation of an expression.
    Furthermore, the prior value shall be accessed only to determine the
    value to be stored."

    > I think that it has something to do with sequence points.


    It does.

    > 1) what is a sequence point ? Why is it defined that way ?


    n869, 5.1.2.3, subparagraph 2:

    "Accessing a volatile object, modifying an object, modifying a file, or
    calling a function that does any of those operations are all side effects,
    which are changes in the state of the execution environment. Evaluation
    of an expression may produce side effects. At certain specified points in
    the execution sequence called sequence points, all side effects of
    previous evaluations shall be complete and no side effects of subsequent
    evaluations shall have taken place."

    > 2) Why is it needed ? What will happen if that restriction is not
    > there ?


    If the restriction were not there, compiler writers would be free to
    take all sorts of shortcuts that would affect the results of some
    programs; sequence points are there so that programmers can be assured
    of getting the same results on any conforming C implementation.

    > 3) Way to find whether any expression violates that rule .


    There is no general way, but if you invoke your compiler with warnings
    turned on, it will probably tell you about practically all suspect
    expressions.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 9, 2006
    #8
  9. int main(void)

    Guest

    int main(void) wrote:

    > I think that it has something to do with sequence points.


    Why did you bother quoting my reply, given that you do not seem to have
    taken anything from it?

    > I posted this question to understand sequence points.


    Then why didn't you say so?

    > I read the FAQ on sequence points.


    Do you mean http://c-faq.com/expr/seqpoints.html ? It's very explicit
    about what your code is trying to do:-

    "A sequence point is a point in time at which the dust has settled and
    all side effects which have been seen so far are guaranteed to be
    complete. The sequence points listed in the C standard are:

    * at the end of the evaluation of a full expression (a full
    expression is an expression statement, or any other expression which is
    not a subexpression within any larger expression);
    * at the ||, &&, ?:, and comma operators; and
    * at a function call (after the evaluation of all the arguments,
    and just before the actual call). "

    It's clear than neither of your examples fit these criteria, as the FAQ
    points out with the reference to the analogous "i = i++;" construction.

    If you'd like a general discussion of Sequence Points, I suggest you
    start a new thread explaining what you are having problems
    understanding about them.
     
    , Oct 9, 2006
    #9
  10. int main(void)

    pete Guest

    int main(void) wrote:

    > int x = 10;


    > x = x + (x = 20);


    That is undefined because neither assignment
    is guaranteed to take place before the other.

    The expression (x = 20), can be evaluated as 20,
    and the sum of 20 and x can be assigned to x,
    prior to the side effect of the assignment taking place
    in the (x = 20) expression.

    The c code statement is modifying an object twice
    without an intervening sequence point.

    ISO/IEC 9899:1999
    6.5 Expressions
    2 Between the previous and next sequence point
    an object shall have its stored value modified
    at most once by the evaluation of an expression.
    Furthermore, the prior value shall be read only to
    determine the value to be stored.

    6.8 Statements and blocks
    4 A full expression is an expression
    that is not part of another expression or of a declarator.

    Annex C
    (informative)
    Sequence points
    1 The following are the sequence points described in 5.1.2.3:
    — The call to a function,
    after the arguments have been evaluated (6.5.2.2).
    — The end of the first operand of the following operators:
    logical AND && (6.5.13);
    logical OR || (6.5.14);
    conditional ? (6.5.15);
    comma , (6.5.17).
    — The end of a full declarator: declarators (6.7.5);
    — The end of a full expression: an initializer (6.7.8);
    the expression in an expression statement (6.8.3);
    the controlling expression of a selection statement
    (if or switch)(6.8.4);
    the controlling expression of a while or do statement (6.8.5);
    each of the expressions of a for statement (6.8.5.3);
    the expression in a return statement (6.8.6.4).
    — Immediately before a library function returns (7.1.4).
    — After the actions associated with each formatted
    input/output function conversion specifier (7.19.6, 7.24.2).
    — Immediately before and immediately after each call
    to a comparison function, and also between any call
    to a comparison function and any movement of the objects
    passed as arguments to that call (7.20.5).

    --
    pete
     
    pete, Oct 10, 2006
    #10
  11. int main(void)

    Thad Smith Guest

    int main(void) wrote:
    > wrote:
    >>int main(void) wrote:


    >>>#include<stdio.h>
    >>>int main(void)
    >>>{
    >>>int x = 10;
    >>>int y = 10;
    >>>
    >>>x = x + (x = 20);
    >>>y = y + y + (y = 20);
    >>>
    >>>printf("x = %d\ny=%d",x,y);
    >>>return 0;
    >>>}
    >>>
    >>> Output is :
    >>> x = 40
    >>> y = 40
    >>>
    >>> Why does the order of evaluation change when there are three
    >>> operands?

    >>
    >>Someone more knowledgable than me will probably quote chapter and verse
    >>from the standard, but as you are altering a value used in a
    >>calculation, you're almost certainly invoking undefined behaviour .


    Correct.

    > I think that it has something to do with sequence points.


    What does "it" refer to?

    > I posted this question to understand sequence points.


    In that case, it helps to ask specific questions about sequence points.

    > I read the FAQ on sequence points.
    > But it is never clear to me no matter how much i read about it.
    > I hope people in this group will help me.
    > The problem i have is
    > 1) what is a sequence point ? Why is it defined that way ?


    Those are questions, not problems.

    A sequence point is a point within a C program that the side effects
    from the previous evaluations have occurred and those of subsequent
    evaluations have not.

    It is defined that way to describe constraints on the translator to
    determine the order in which the program side effects occur.

    > 2) Why is it needed ?


    Without a definition of sequence point, it becomes harder to describe
    the constraints on the translator to arrange code, while providing the
    programmer guarantees of the resulting code arrangement.

    > What will happen if that restriction is not there ?


    Which restriction?

    > 3) Way to find whether any expression violates that rule .


    What rule? A definition is not a rule. Perhaps you are thinking of the
    following constraint:
    6.5p2: Between the previous and next sequence point an object
    shall have its stored value modified at most once by the
    evaluation of an expression. Furthermore, the prior value
    shall be accessed only to determine the value to be
    stored.

    In order to know whether an expression violates the constraint, you need
    to know at which points sequence points occur. There are also some
    subtleties with "shall be accessed only to determine the value
    to be stored".

    Look at the examples in section 6.5 of the standard. You can purchase a
    copy of it from ANSI (or other ISO-associated national standard
    organizations) or download a recent draft version as n1124.pdf for free
    (highly recommended). There is also a hardcopy book containing the
    standard, plus commentary, I believe.

    --
    Thad
     
    Thad Smith, Oct 10, 2006
    #11
    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. Nan Li

    evaluation order

    Nan Li, Nov 14, 2005, in forum: Java
    Replies:
    11
    Views:
    653
    Oliver Wong
    Nov 15, 2005
  2. Xavier Decoret

    Evaluation order for a=b

    Xavier Decoret, Jul 3, 2003, in forum: C++
    Replies:
    1
    Views:
    347
    Ron Natalie
    Jul 3, 2003
  3. Ilias Lazaridis
    Replies:
    2
    Views:
    392
    Ilias Lazaridis
    Apr 24, 2005
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    763
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    334
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page