Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!dimacs.rutgers.edu!bcm!shell!shell!rjohnson From: rjohnson@shell.com (Roy Johnson) Newsgroups: comp.lang.c Subject: Re: Problem with post-incrementation Message-ID: Date: 17 Apr 91 15:38:52 GMT References: <6530@s3.ireq.hydro.qc.ca> <20095@ogicse.ogi.edu> Sender: usenet@shell.shell.com (USENET News System) Organization: Shell Development Company, Bellaire Research Center, Houston, TX Lines: 23 In-Reply-To: vegdahl@ogicse.ogi.edu's message of 16 Apr 1991 13:35:34 GMT In article <20095@ogicse.ogi.edu> vegdahl@ogicse.ogi.edu (Steve Vegdahl) writes: >>static int i = 0; >>static char str[256]; >> >> sprintf(str, "tmp_%d", i = (i++) % 3600); >The problem is that the expression > i = (i++) % 3600) >applies two side-effects to the variable i, without any intevening "sequence >points". This results in "undefined behavior", according to the ANSI standard. >(Just be glad that it didn't erase your disk!) >I think what you really want is sprintf(str, "tmp_%d", (i++) % 3600); >though this will still likely lead to unexpected behavior if/when i overflows. I think what he really wants is sprintf(str, "tmp_%d", i = (i+1) % 3600); but only he can know for sure. -- =============== !You!can't!get!here!from!there!rjohnson =============== Feel free to correct me, but don't preface your correction with "BZZT!" Roy Johnson, Shell Development Company