[comp.sys.sgi] Fun and games with ++ operators

mike@SNOWHITE.CIS.UOGUELPH.CA (09/21/90)

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@snowhite.cis.uoguelph.ca

nong@MCC.COM (09/22/90)

>		#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@mcc.com

murray@vs2.scri.fsu.edu (John Murray) (09/25/90)

In article <9009211942.AA07458@banach.aca.mcc.com> nong@mcc.com writes:
>
>>		#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++]);
>>		}
>
>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.

#include <stdio.h>
int a [3];
main()
{
    int i;
    a[0] = 5;
    a[1] = 6;
    a[2] = 7;
    i = 0;
    printf("%d %d %d\n", a[++i], a[++i], i);
    printf("%d\n", i);
}

output:
7 7 2
2

(output with - printf("%d %d %d\n", a[i++], a[i++], i); - was 5 5 0)

Hyuk, Hyuk, Hyuk....

240GTX / 3.3 / mips version 2.0

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

John R. Murray          | "They call me Mr. Know-it-all, I am so eloquent.
murray@vs2.scri.fsu.edu |  Perfection is my middle name!
                        |   ...and whatever rhymes with 'eloquent'." - Primus

pkr@sgi.com (Phil Ronzone) (09/29/90)

In article <9009211425.AA04291@snowhite.cis.uoguelph.ca> mike@SNOWHITE.CIS.UOGUELPH.CA writes:
>			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.


Such behaviour is ill-defined and I thought that K&R had an explicit warning
about it. I.e., don't do that kind of stuff, especially in passing arguments!


--
<---------------------------------------------------------------------------->
Philip K. Ronzone                  S e c u r e   U N I X           pkr@sgi.com
Silicon Graphics, Inc. MS 9U-500                           work (415) 335-1511
2011 N. Shoreline Blvd., Mountain View, CA 94039            fax (415) 969-2314

bh@sgi.com (Bent Hagemark) (09/29/90)

In article <1990Sep28.223417.7571@odin.corp.sgi.com> pkr@sgi.com (Phil Ronzone) writes:
>In article <9009211425.AA04291@snowhite.cis.uoguelph.ca> mike@SNOWHITE.CIS.UOGUELPH.CA writes:
>>			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.
>
>
>Such behaviour is ill-defined and I thought that K&R had an explicit warning
>about it. I.e., don't do that kind of stuff, especially in passing arguments!
>
>
>--
><---------------------------------------------------------------------------->
>Philip K. Ronzone                  S e c u r e   U N I X           pkr@sgi.com
>Silicon Graphics, Inc. MS 9U-500                           work (415) 335-1511
>2011 N. Shoreline Blvd., Mountain View, CA 94039            fax (415) 969-2314

The warning just happens to be on the most important page of K&R C.
For the 2nd edition this is page 53.  I know this because a copy of it
is taped to my office wall right next to my screen!

Bent

mitch@sgi.com (Thomas Mitchell) (10/23/90)

In article <1990Sep28.224937.7838@odin.corp.sgi.com> bh@sgi.com (Bent Hagemark) writes:
* In article <1990Sep28.223417.7571@odin.corp.sgi.com> pkr@sgi.com (Phil Ronzone) writes:
* >In article <9009211425.AA04291@snowhite.cis.uoguelph.ca> mike@SNOWHITE.CIS.UOGUELPH.CA writes:
* >>			printf("%d %d\n", a[i++], a[i++]);
* >>    Depending on the order of evaluation of function arguments, ...

* 
* The warning just happens to be on the most important page of K&R C.
* For the 2nd edition this is page 53.  I know this because a copy of it
* is taped to my office wall right next to my screen!
* 
* Bent


And under the keyboard of another engineer is a Xerox(tm) of
the Precedence and Order of Evaluation table from page 49 of
K&R (1978).

--
--
  Thomas P. Mitchell   --  mitch@sgi.com  or mitch%relay.csd@sgi.com
	"All things in moderation; including moderation."