Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!stjhmc!f14.n15.z1.fidonet.org!Dave.Harris From: Dave.Harris@f14.n15.z1.fidonet.org (Dave Harris) Newsgroups: comp.lang.c Subject: post-decrement quirk? Message-ID: <14467.2860B4F2@stjhmc.fidonet.org> Date: 20 Jun 91 13:57:02 GMT Sender: ufgate@stjhmc.fidonet.org (newsout1.26) Organization: FidoNet node 1:15/14 - Nibbles 'n Bits, Orem UT Lines: 64 >From: julbro@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 >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@f14.n15.z1.fidonet.org