More Re: Function Argument Evaluation argument

Paul D. Smith pds at lemming.webo.dg.com
Wed Apr 3 14:39:27 AEST 1991


  [ Hopefully I understand what you're saying here; if not please correct
    me... ]

[] On 2 Apr 91 13:24:16 GMT, volpe at camelback.crd.ge.com (Christopher R Volpe) said:

CRV> In article <RJOHNSON.91Apr1142143 at olorin.shell.com>, rjohnson at shell.com
CRV> (Roy Johnson) writes:
CRV> |>
CRV> |>   int v=1;
CRV> |>   printf("%d %d\n", (1, v), (1,v++));
CRV> |>
CRV> |>This can print
CRV> |>	1 1
CRV> |>or
CRV> |>	2 1

CRV> Hey, that looks pretty good. No matter what the order of
CRV> evaluation of the function arguments is, there's always a
CRV> sequence point separating the references to v. So the behavior is
CRV> not undefined, yet order of evaluation definitely has a drastic
     ^^^^^^^^^^^^^
CRV> effect on the output.

The behavior *is* indeed undefined in this case:

Reference 1: ANSI X3.159-1989 Section 2.1.2.3, p. 8, lines 33-35:

   "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."

Reference 2: Ibid, Section 3.3.2.2, p. 42, lines 20-21:

   "The order of evaluation of the function designator, the arguments,
        ^^^^^^^^^^^^^^^^^^^
    and subexpressions within the arguments is unspecified, ... "
                                               ^^^^^^^^^^^

So given the following statement:

    printf("%d %d\n", (1, v), (1, v++));
                      ^^^^^^  ^^^^^^^^
                      exp. 1   exp.2

The compiler is free to choose whether to evaluate exp.1 first, or
whether to evaluate exp.2 first.  Once it chooses it must indeed
evaluate the "1" first, before the "v" or "v++".

So it's still undefined whether this prints "1 1" or "2 1".  And
rightly so, IMHO!

                                                                paul
-----
 ------------------------------------------------------------------
| Paul D. Smith                          | pds at lemming.webo.dg.com |
| Data General Corp.                     |                         |
| Network Services Development Division  |   "Pretty Damn S..."    |
| Open Network Systems Development       |                         |
 ------------------------------------------------------------------



More information about the Comp.std.c mailing list