Evaluating autoincrements in function calls

welch%UMASS.BITNET at wiscvm.wisc.EDU welch%UMASS.BITNET at wiscvm.wisc.EDU
Fri Feb 27 03:21:51 AEST 1987


Here's an interesting C error/mis-feature I'd like to bring to your
attention as a possible portability problem.
 
int    set[]={1,2,3},
    i = 0;
main()
{
printf("\nnumbers in set indexed by incr integer = %d,%d,%d",
    set[i++],
    set[i++],
    set[i++]);
 
printf("\nnumbers in set indexed by constants = %d,%d,%d,
    set[0],
    set[1],
    set[2]);
}
 
The lattice C (V2.15) compiler produces:
 
numbers in set indexed by incr integer = 1,2,3
numbers in set indexed by constants = 1,2,3
 
as output, while the Microsoft (V4.0) and VAX C (V2.2-015) compilers produce:
 
numbers in set indexed by incr integer = 3,2,1
numbers in set indexed by constants = 1,2,3
 
 
In the second case the compiler is evaluating the arguments to
the printf function right to left, including the autoincrement
instructions, as it builds the stack.
 
Lattice evaluates the autoincrement instructions left to
right before pushing the arguments in the function call on the stack
(from right to left).
 
I don't want to be swamped with replies as to which set of results
is correct but I would be interested to know if there is a standard
which documents the correct behavior of argument evaluation.
 
Jonathan Welch
 
Bitnet: jhwelch at amherst



More information about the Comp.unix.questions mailing list