Fun and games with ++ operators

mike at SNOWHITE.CIS.UOGUELPH.CA mike at SNOWHITE.CIS.UOGUELPH.CA
Sat Sep 22 00:25:43 AEST 1990


Greetings and obeisances, O SGI sages!

    I have found a peculiarity in the MIPS C compiler that is more of a 
curiosity than a problem, but I wonder if someone could shed some light on
it anyway, just for fun.

    Here is a sample program that exhibits the peculiar behavior:
		#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++]);
		}
    Depending on the order of evaluation of function arguments, I would expect
this code to generate "6 5" or "5 6".  But instead it says "5 5", which 
confuses me a bit.  Do all pre/post increment/decrement operators only
take effect after the current source line, or what?  I did check the value
of 'i' on the next line, and it was indeed 2, so that part works.  For C
reference 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.

    This phenomenon is no skin of my shins, as I have always felt that overuse
of these operators is bad form anyways.  But it would be nice to know just 
what the official line is on this case, and how far we can expect optimizing
compilers to stretch our assumptions about language semantics.

    Thanks for not laughing too much.

World's shortest SF story:           |  Mike Chapman
"Aliens disguised as workstations?   |  Grab Student, University of Guelph
 I never heard such--"               |  mike at snowhite.cis.uoguelph.ca



More information about the Comp.sys.sgi mailing list