Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!ksr!jfw From: jfw@ksr.com (John F. Woods) Newsgroups: comp.lang.c Subject: Re: Heroic failures (q = q++) Message-ID: <4210@ksr.com> Date: 25 Jun 91 16:08:44 EDT References: <902@adimail.UUCP> <7079@gara.une.oz.au> <15520@exodus.Eng.Sun.COM> <1991Jun25.151408.1024@ux1.cso.uiuc.edu> Sender: news@ksr.com Lines: 33 vulcan@uiuc.edu (EvilTwin) writes: >In article <902@adimail.UUCP> tel@adimail.UUCP (Terry Monks) writes: >>(Peter van der Linden (linden@adapt.Eng.sun.com)) scripsit: >>> q = q++; EWW! >A co-worker and I tried the above on our respective machines. He on an IBM >70/386 using Borland C++, I on a MicroVAX II using DEC's C version 3.1. He >received a result of 6, I one of 5. Why is this? Why is this? You used two different compilers to compile an undefined expression. Why 5 and 6? 5 is the reasonable result of compiling into code like TMP := q ; value of postincrement is old value q := q+1 ; do postincrement q := TMP ; do assignment and 6 corresponds to TMP := q ; value of postincrement is old value q := TMP ; do assignment q++ ; take care of postincrements left over in expression Depending on the actual hardware and software architecture, either of these is a perfectly reasonable approaches to take (of course, a decent compiler would probably be able to reduce them to zero or one machine instructions respectively). As to why it is undefined, read The Standard and the FAQ, alternately, until either you reach enlightenment or your head explodes. Note that because it is *undefined*, the compiler was perfectly at liberty to generate q := 666 ; special marker for evil constructs or JSR _cpu$detonate or any other damned thing it liked.