Fun and games with ++ operators

nong at MCC.COM nong at MCC.COM
Sat Sep 22 05:42:32 AEST 1990


>		#include <stdio.h>
>		int a [2];
>		main()
>		{
>			int i;
>			a[0] = 5;
>			a[1] = 6;
>			i = 0;
>			printf("%d %d\n", a[i++], a[i++]);
>		}
.....
>.....  I only have the ancient K&R, which as far as I can tell does not
> formally specify when these operators are supposed to do their thing.  For
> reference purposes, I ran this code on many platforms (6 different CPUs, 
> 8 different compilers), and only the MIPS compiler came out with the "5 5"
> answer.


According to K&R ( p 59 1st edition, or 63 2nd edition )
" The commas that separate function arguments, variables in declarations,
etc., are not comma operators, and do not guarantee left to right evaluation."

In this case the arguments may be evaluated in parallel.  It'll be interesting
to see if i is printed as the last variable in the same print statment, will
it be 0.

______________
Nong Tarlton
Microelectonics and Computer Technology Corporation
nong at mcc.com



More information about the Comp.sys.sgi mailing list