post-decrement quirk?
Dave Harris
Dave.Harris at f14.n15.z1.fidonet.org
Thu Jun 20 23:57:02 AEST 1991
>From: julbro at auto-trol.com (Julie Brown)
>Can anyone explain to me why the following does not work
>(at least on the Sun):
> q = q--;
As I have been reminded several times in the last week myself, this statement
is undefined. Modifing an address multiple time between sequence points is
undefined. Both pre and post yeild 4 when I try this with what I use (TC++).
Being undefined, your machine could yeild or do anything including crash on
this statement (so I've been told). q-- all by its lonesome self is
sufficient.
>'q' does not get decremented. No matter what the order of
>operations, I would expect 'q' to get decremented at some
>point. It doesn't make a whole lot of sense to do this until
>you put it in a more meaningful context:
In machine language terms your compiler is possibly doing something like:
R1 = Q
Dec Q
Q = R1
where Q your variable and R1 is a register. Just speculating of course.
Fiddle with the optimizer switches and see if it acts differently.
>#include <stdio.h>
>main()
>{
> int count = 5;
> count = (count > 0) ? count-- : 0;
> printf("count %d\n", count);
>}
>the output from this program is :
> count 5
count = (count > 0) ? count-1 : 0;
count = max(0,count-1);
count -= (count>0);
no shortage of ways to do this.
>pre-increment works just fine (q = --q;).
Dec Q
R1 = Q
Q = R1
Speculating again of course.
--
Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!15!14!Dave.Harris
Internet: Dave.Harris at f14.n15.z1.fidonet.org
More information about the Comp.lang.c
mailing list