[net.lang.c] Evaluation of function arguments

LINNDR%VUENGVAX.BITNET@wiscvm.ARPA (09/22/86)

Does anyone know of a compiler for which the program below does NOT
produce the output shown?
----------------------------------
/* order - program to demonstrate order of function argument evaluation */
/*      David Linn - Vanderbilt University - 1986                       */
main()
{
        int i;

        i = 0;
        auxfun(i++,i++,i++);
        printf("%d\n",i);
}

auxfun(a,b,c)
{
        printf("%d,%d,%d\n",a,b,c);
}
----- PROGRAM OUTPUT ---------------
2,1,0
3
------------------------------------

David Linn
LINNDR%VUENGVAX.BITNET@WISCVM.WISC.EDU          ! Internet (I think)
...!psuvax1!vuengvax.bitnet!linndr              ! Usenet

phil@osiris.UUCP (Philip Kos) (09/25/86)

The C compiler for Pyramid OSx version 3.1 produces the results

0,1,2
3

So there's another one for your list...


Phil Kos			  ...!decvax!decuac
The Johns Hopkins Hospital                         > !aplcen!osiris!phil
Baltimore, MD			...!allegra!umcp-cs

"And if you want to put your feet in a toilet, remember, what's a
democracy for?"  - Paul Krassner

wsmith@uiucdcsb.cs.uiuc.edu (09/26/86)

>int i;
> i = 0;
>f ( i++, i++, i++);
>write(i);
> f( a,b,c) int a,b,c;
>  write (a,b,c);
>
Because the expression being used in your program is un-defined in
the C language, lint complains about such non-sensical programs,
saying evaluation order undefined.

Your program could be translated into something like this:
	load   r0,i
	pusharg r0
	load   r1,i
	pusharg r1
	load   r2,i
	pusharg r2
	inc    r2
	load   i,r2
	inc    r1
	load   i,r1
	inc    r0
	load   i,r0
	call	f
	....


In which case, the program would print out:
0 0 0
1
Another possibility that will probably generate fewer flames is:
0 0 0
3

I don't know of a compiler that does such nonsense, but I think one could.

A safer program is
main()
{
int i;
f(g(&i),g(&i),g(&i));
write(i);
}
f(a,b,c)  int a,b,c;
{ write (a,b,c); }
g(pi) int * pi;  
{ return( (*pi)++ ); }

Bill Smith
ihnp4!uiucdcs!wsmith
wsmith@a.cs.uiuc.edu

robison@uiucdcsb.cs.uiuc.edu (09/27/86)

[I tried to mail this response, but wasn't able.]

If you look at page 212 of Kernighan and Ritchie, (Appendix A, section 16) it
notes that the order of evaluation is right to left on the PDP-11, and left to
to right on the others.  I presume the others are the Honeywell 6000, IBM 370,
and Interdata 8/32 which are mentioned in Appendix A.  Of course that was almost
a decade ago.

Arch D. Robison					robison@uiucdcs
University of Illinois at Urbana-Champaign