Problem with post-incrementation

Simon Parsons simonp at fulcrum.bt.co.uk
Thu Apr 18 01:19:51 AEST 1991


In article <6530 at s3.ireq.hydro.qc.ca> godin at 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 at fulcrum.bt.co.uk



More information about the Comp.lang.c mailing list