Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!mintaka!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!axion!uzi-9mm.fulcrum.bt.co.uk!simonp From: simonp@fulcrum.bt.co.uk (Simon Parsons) Newsgroups: comp.lang.c Subject: Re: Problem with post-incrementation Message-ID: <1!{_*K$@uzi-9mm.fulcrum.bt.co.uk> Date: 17 Apr 91 15:19:51 GMT References: <6530@s3.ireq.hydro.qc.ca> Sender: news@fulcrum.bt.co.uk (News with an UZI) Organization: BT Fulcrum, Birmingham Lines: 34 In article <6530@s3.ireq.hydro.qc.ca> godin@ireq.hydro.qc.ca (Andre Godin 8926) writes: > > Every times I call this function, the value of 'str' > is always tmp_0 and 'i' never gets incremented. If > we use the pre-increment operator, the function works > properly. Why post-incrementing doesn't work? > > (I'm using cc under SunOs 4.0.3.) > >char * >fnc() /* >----- */ >{ >static int i = 0; >static char str[256]; > > sprintf(str, "tmp_%d", i = (i++) % 3600); > > return (str); >} No suprises there the expression on the right-hand side of the assignment is evaluated to zero, i is post-incremented to 1. Then the value of the expression ( 0 ) is assigned to i. Hence i remains at zero for each call of the function. Simon -- "Hey girl, as I've always said, I prefer your lips red, Not what the good lord made, but what he intended." Simon Parsons, BT Fulcrum - simonp@fulcrum.bt.co.uk