Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!hsdndev!cmcl2!lanl!cochiti.lanl.gov!jlg From: jlg@cochiti.lanl.gov (Jim Giles) Newsgroups: comp.lang.c Subject: Re: Problem with post-incrementation Message-ID: <21400@lanl.gov> Date: 15 Apr 91 17:26:28 GMT References: <6530@s3.ireq.hydro.qc.ca> Sender: news@lanl.gov Organization: Los Alamos National Laboratory Lines: 19 sprintf(str, "tmp_%d", i = (i++) % 3600); I have seen this bug in a number of C programs (including the _second_ edition of a published book on graphics in C). It is one of the drawbacks of side-effect operators that such errors are made and remain undetected. The problem is that i = (i++) etc. assigns the value of the expression to the variable i. Depending on the order that the side-effects are carried out, this may happen before or after the side-effect for the incrementation operator. If the assignment happens _after_ the increment, then i will get zero every time. What you want to do is this: sprintf(str, "tmp_%d", i = (i+1) % 3600); You only need i to be assigned once anyway. J. Giles