difference between c++; and ++c;

Berry;Craig D. berry at arcturus.uucp
Fri Apr 19 09:05:59 AEST 1991


bhoughto at nevin.intel.com (Blair P. Houghton) writes:

>In article <389 at civet.ukc.ac.uk> mtr at ukc.ac.uk (M.T.Russell) writes:
>>Notionally we have:
>[...I have no joke here, I just like the proper use of the word "notionally..."]
>[ :-) and I need a little inews-inclusion-meter fodder :-( ]
>>   i++;	  "Save the current value of i, increment i then discard
>>	   the value just saved"
>>   ++i;   "Increment i then discard the resulting value"
>>Obviously most compilers will generate exactly the same code for
>>either case, but it is nice to express what you mean as directly as
>>possible.

>    i += 1;	"Increment i."

No, "Increment i then discard the resulting value."  Could it be that you
have forgotten that += is an operator, returning the new lhs rvalue?  For
example,

	int a = 5;
	int b;

	b = (a += 1);

is perfectly valid, leaving a and b equal to 6.  My conclusion:  "++c" and
"c += 1" are semantically equivalent, and "++c" is shorter.  My vote goes
for the latter.



More information about the Comp.lang.c mailing list