Problem with post-incrementation

Roy Johnson rjohnson at shell.com
Thu Apr 18 01:38:52 AEST 1991


In article <20095 at ogicse.ogi.edu> vegdahl at 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



More information about the Comp.lang.c mailing list