Sequence points and order of evaluation

Walter Murray walter at hpcllca.HP.COM
Fri Aug 5 05:42:24 AEST 1988


First, a warm-up:

int f(i) {printf ("%d",i); return i;}
main ()
{
   (f(1)+f(2)) + (f(3)+f(4));
   printf ("\n");
}

1.  Is this a strictly conforming program?  (I think NO, because
    it produces output dependent on an unspecified order of
    evaluation.)
2.  Is a conforming implementation required to produce a diagnostic
    for this program?  (I think NO, because the program doesn't
    violate a syntax rule or constraint.)
3.  Is a conforming implementation even required to accept
    this program?  (Technically, I think NO, because it's not
    a strictly conforming program.)
4.  At execution, may a conforming implementation produce the
    output "1324" for this program?  (I don't know.  If it's not
    a strictly conforming program, does the dpANS impose any
    requirements at all on a conforming implementation?)

Now for the real question:

main ()
{
   int temp, result;
   result = (temp=1, temp) + (temp=2, temp);
   printf ("result = %d\n", result);
}

5.  Is this a strictly conforming program?
6.  What output will this program produce on a conforming
    implementation?  (I've tried it on six compilers from
    three vendors, and they all produce output which seems
    wrong to me.)


(The second program was inspired by a posting
to comp.lang.c by Jeffrey E. F. Friedl.)

Walter Murray
All opinions expressed here are my own.



More information about the Comp.std.c mailing list