difference between c++; and ++c;

Tim Olson tim at proton.amd.com
Wed Apr 10 01:21:39 AEST 1991


In article <1991Apr08.161444.10025 at cs.ruu.nl> hnridder at cs.ruu.nl (Ernst de Ridder) writes:
| I once read (I'm not sure where, but I believe it was in the C++
| programming language) that some C-programmers argue that ++c is neater
| than c++.  (In situations where the side-effect doesn't matter).  So
| they write (just as an illustration)
| 	while ( c < 100)
| 	   ++c;
| instead of
| 	while ( c < 100)
| 	   c++;
| 
| Why should one of these forms be preferred over the other in such a situation,
| apart from personal preferences?

Well, it mainly *is* personal preference.  A reasonable compiler
should generate the same code sequence for either expression.  The
reason many prefer the prefix operator rather than the postfix
operator is that postfix operations are somewhat special -- the
resultant value is that of the lvalue *before* the side-effect
operation, rather than the result of the operation.  Because of this
difference, many use the postfix operators only when this action is
required.



--
	-- Tim Olson
	Advanced Micro Devices
	(tim at amd.com)



More information about the Comp.lang.c mailing list