[comp.lang.c] difference between c++; and ++c;

hnridder@cs.ruu.nl (Ernst de Ridder) (04/08/91)

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?

Ernst de Ridder

-- 
	Qualitas qualitatem inducit. Semper ego qualitatem.

popa
iret

razdan@phx.mcd.mot.com (Anshuman Razdan) (04/09/91)

In article <1991Apr08.161444.10025@cs.ruu.nl> hnridder@cs.ruu.nl (Ernst de Ridder) writes:

   Path: mcdphx!asuvax!cs.utexas.edu!uunet!mcsun!hp4nl!alchemy!hnridder
   From: hnridder@cs.ruu.nl (Ernst de Ridder)
   Newsgroups: comp.lang.c,comp.std.c
   Summary: Why write ++c; instead of c++

   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?

   Ernst de Ridder
------------------------- -------------------------

I believe the difference would be seen if it were ..

	while(++c < 100)
	  .....;

vs
	while(c++ < 100)
	 .....;

Rest is personal preference.

--

Anshuman Razdan

************************************************************
* razdan@toy			Test and Methodology Group *
*							   *
* razdan@phx.mcd.mot.com	Diablo Plant, Tempe  Az    *
************************************************************

xor@aix01.aix.rpi.edu (Joseph Schwartz) (04/09/91)

In article <1991Apr08.161444.10025@cs.ruu.nl> hnridder@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, in the above example, it doesn't really matter whether you use c++ or
++c.  However, say you had this instead:
 
     while (*pFoo < 100)
       ++*pFoo;
 
I prefer to preincrement, because postincrement would require some parens:
 
     while (*pFoo < 100)
       (*pFoo)++;
 
So in general, I will preincrement wherever I have a choice, to avoid making
the mistake of leaving out the above parentheses.

-- 
Joe Schwartz
Internet: xor@mts.rpi.edu
Bitnet: userez3n@rpitsmts

john@iastate.edu (Hascall John Paul) (04/09/91)

hnridder@cs.ruu.nl (Ernst de Ridder) asks why:
}	   ++c;
}instead of
}	   c++;

   Consider for a moment (yes, these are not equivalent):

           x = ++c;       vs       x == c++;

These can be "compiled" as:

                                   temp_001 <-- c;
           c <-- c + 1;            c <-- c + 1;
           x <-- c;                x <-- temp_001;

(now throw away the "x=" part (last "instruction").

So, "++c" is ``cleaner'' in some pedantic sense[1], and I suppose a
sufficiently lacking compiler might actually produce slower code
for "c++;" than for "++c;".

John

[1] I am one of those "guilty" of using "++c;"
--
John Hascall                        An ill-chosen word is the fool's messenger.
Project Vincent
Iowa State University Computation Center                       john@iastate.edu
Ames, IA  50011                                                  (515) 294-9551

scott@bbxsda.UUCP (Scott Amspoker) (04/09/91)

In article <1991Apr08.161444.10025@cs.ruu.nl> hnridder@cs.ruu.nl (Ernst de Ridder) writes:
>	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?

Apart from personal preference - none.  Some people (like myself) prefer
the first form (++c) because the second form (c++) *implies* preserving
the current value of c prior to incrementing.  Sure, that really doesn't
happen but you know how wierd programmers can be.

-- 
Scott Amspoker                       | Touch the peripheral convex of every
Basis International, Albuquerque, NM | kind, then various kinds of blaming
(505) 345-5232                       | sound can be sent forth.
unmvax.cs.unm.edu!bbx!bbxsda!scott   |    - Instructions for a little box that
                                     |      blurts out obscenities.

mtr@ukc.ac.uk (M.T.Russell) (04/09/91)

In article <1991Apr08.161444.10025@cs.ruu.nl> hnridder@cs.ruu.nl (Ernst de Ridder) writes:
>[Why is ++i better than i++ when the value is unused]

Because it is a more direct expression of the desired operation.
Notionally we have:

   i++;	  "Save the current value of i, increment i then discard
	   the value just saved"

versus:

   ++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.

Mark

gwyn@smoke.brl.mil (Doug Gwyn) (04/09/91)

In article <1991Apr08.161444.10025@cs.ruu.nl> hnridder@cs.ruu.nl (Ernst de Ridder) writes:
>Why should one of these forms be preferred over the other in such a situation,
>apart from personal preferences?

Some of us find it clearer to treat ++ and -- unary operators the same
way as other unary operators, namely apply them from the left:
	!x	not	x!
	++x	not	x++
This is a matter of style, but that does not mean that it is arbitrary.
(Although in this case I would say that it doesn't matter a whole lot.)

bhoughto@nevin.intel.com (Blair P. Houghton) (04/09/91)

In article <389@civet.ukc.ac.uk> mtr@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."

				--Blair
				  "Notionally, 'mu.'"

coop4y44@bwdla28.bnr.ca (Takis Skagos) (04/09/91)

In article <1991Apr08.161444.10025@cs.ruu.nl> hnridder@cs.ruu.nl (Ernst de Ridder) writes:
>	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, ++c is incrimented before c is used and c++ is used before it
is incrimented.  As you're using it, it doesn't matter, but here is a
sample program that you can try:

main()
{
  int a,b,c;
  a=b=0;
  for (c=0;c<=40;c++)
    printf(" a := %4d\tb:= %4d\n",a++,++b);
}

  When you execute the program you will get:
 a :=    0      b:=    1
 a :=    1      b:=    2
 a :=    2      b:=    3
           ... 
 a :=   38      b:=   39
 a :=   39      b:=   40
 a :=   40      b:=   41

  So you see, 'a' is used before it is incrimented and 'b' is incrimented
before it is used.  In the 'for' statement, it doesn't matter whether
we use '++c' or 'c++'.

                                             Taki

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
BNR Ottawa           Disclaimer:  "I swear, they made me do it!"
P. Takis Skagos 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

tim@proton.amd.com (Tim Olson) (04/09/91)

In article <1991Apr08.161444.10025@cs.ruu.nl> hnridder@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@amd.com)

miller@GEM.cam.nist.gov (Bruce R. Miller) (04/10/91)

In article <1991Apr08.161444.10025@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
			    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If the side-effect doesn't matter, I prefer to use this:

(ie. whitespace).  Most compilers do a good job of optimizing it.  :>>  

But seriously, I think you meant to say,
   "where the VALUE doesn't matter" 
In which case, I can't think of any particular reason other than
personal preference.  and possible the precedence business that another
poster mentioned...

wittig@gmdzi.gmd.de (Georg Wittig) (04/10/91)

So, is

	``c++''		always equivalent to	``(++c - 1)''

?
-- 
Georg Wittig   GMD-Z1.IT   P.O.Box 1240 | "Freedom's just another word
D-W-5205 St. Augustin 1	   (Germany)	|  for nothing left to lose"
email:		wittig@gmdzi.gmd.de	| (from "Me and Bobby McGee",
telephone:	(+49) 2241 14-2294	|  Janis Joplin, Kris Kristofferson)

diamond@jit345.swstokyo.dec.com (Norman Diamond) (04/10/91)

In article <3730@inews.intel.com> bhoughto@nevin.intel.com (Blair P. Houghton) writes:
>In article <389@civet.ukc.ac.uk> mtr@ukc.ac.uk (M.T.Russell) writes:
>>   i++;	  "Save the current value of i, increment i then discard
>>	   the value just saved"
>>   ++i;   "Increment i then discard the resulting value"
>
>    i += 1;	"Increment i."
Nope; it's
     i += 1;    "Increment i then discard the resulting value"

(as opposed to  j = (i += 1);  "Increment i then use the resulting value")

Follow-ups to comp.lang.c.
--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.

terryh@ukcsd.uk.sun.com (Terry Heatlie - Sun UK - Tech Support ) (04/10/91)

In article <3730@inews.intel.com>, bhoughto@nevin.intel.com (Blair P.
Houghton) writes:
|> In article <389@civet.ukc.ac.uk> mtr@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."
Of course the value of i+=1 is 1, so this is really
"increment i and discard the resulting value"
|> 
|> 				--Blair
|> 				  "Notionally, 'mu.'"

Regards,
Terry Heatlie.

Disclaimer:  all my own work (except this disclaimer, which I nicked).

volpe@camelback.crd.ge.com (Christopher R Volpe) (04/10/91)

In article <2880223615@ARTEMIS.cam.nist.gov>, miller@GEM.cam.nist.gov
(Bruce R. Miller) writes:
|>
|>In article <1991Apr08.161444.10025@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
|>			    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|>If the side-effect doesn't matter, I prefer to use this:
|>
|>(ie. whitespace).  Most compilers do a good job of optimizing it.  :>>  

If the side effect doesn't matter and the value *does* matter, you're
better off using "c + 1". Granted, it doesn't optimize into nothing quite
as easily, but it does produce more accurate results. :-) :-)
               
==================
Chris Volpe
G.E. Corporate R&D
volpecr@crd.ge.com

cs450a03@uc780.umd.edu (04/11/91)

Georg Wittig writes:
>So, is
>	``c++''		always equivalent to	``(++c - 1)''

I think you have to worry about a couple of boundary conditions here,
with floating point numbers, and maybe with signed integers.

Raul Rockwell

diamond@jit345.swstokyo.dec.com (Norman Diamond) (04/11/91)

In article <3614@texsun.Central.Sun.COM> terryh@ukcsd.uk.sun.com (Terry Heatlie - Sun UK - Tech Support ) writes:
>|> >   i++;	  "Save the current value of i, increment i then discard
>|> >	   the value just saved"
>|> >   ++i;   "Increment i then discard the resulting value"
>|>     i += 1;	"Increment i."
>
>Of course the value of i+=1 is 1,

W       W    RRRR      OOOO     N   N     GGGGG
W   W   W    R   R    O    O    NN  N    G  G
 W W W W     RRRR     O    O    N  NN    G    G
  W   W      R   R     OOOO     N   N     GGGG

>so this is really "increment i and discard the resulting value"
(Yes, but.)
--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.

diamond@jit345.swstokyo.dec.com (Norman Diamond) (04/11/91)

In article <4496@gmdzi.gmd.de> wittig@gmdzi.gmd.de (Georg Wittig) writes:

>So, is  ``c++''  always equivalent to  ``(++c - 1)''  ?

Usually.  In cases of overflow, sometimes yes, sometimes not necessarily.
--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.

terryh@ukcsd.uk.sun.com (Terry Heatlie - Sun UK - Tech Support ) (04/11/91)

In article <1991Apr11.011347.5136@tkou02.enet.dec.com>,
diamond@jit345.swstokyo.dec.com (Norman Diamond) writes:
|> In article <3614@texsun.Central.Sun.COM> terryh@ukcsd.uk.sun.com
(Terry Heatlie - Sun UK - Tech Support ) writes:
|> >|> >   i++;	  "Save the current value of i, increment i then discard
|> >|> >	   the value just saved"
|> >|> >   ++i;   "Increment i then discard the resulting value"
|> >|>     i += 1;	"Increment i."
|> >
|> >Of course the value of i+=1 is 1,
|> 
|> W       W    RRRR      OOOO     N   N     GGGGG
|> W   W   W    R   R    O    O    NN  N    G  G
|>  W W W W     RRRR     O    O    N  NN    G    G
|>   W   W      R   R     OOOO     N   N     GGGG
|>
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Guilty.  Try "1 more than i was to start with" 
|> >so this is really "increment i and discard the resulting value"
|> (Yes, but.)
|> --
|> Norman Diamond       diamond@tkov50.enet.dec.com
|> If this were the company's opinion, I wouldn't be allowed to post it.

Regards,
Terry Heatlie.

Disclaimer:  all my own work (except this disclaimer, which I nicked).

mikes@ingres.com (Mike Schilling) (04/11/91)

We old folks use:

	c++;
	--c;
----------------------------------------------------------------------------
mikes@rtech.com = Mike Schilling, ASK Corporation, Alameda, CA
Just machines that make big decisions,
Programmed by fellows with compassion and vision.	-- Donald Fagen, "IGY"

jrbd@craycos.com (James Davies) (04/12/91)

In article <4496@gmdzi.gmd.de> wittig@gmdzi.gmd.de (Georg Wittig) writes:
>
>So, is
>
>	``c++''		always equivalent to	``(++c - 1)''
>
>?

Not if you precede it with

	#define c a+b

diamond@jit345.swstokyo.dec.com (Norman Diamond) (04/12/91)

In article <1991Apr11.151933.25867@ingres.Ingres.COM> mikes@ingres.com (Mike Schilling) writes:
>We old folks use:
>	c++;
>	--c;

Some of us older folks didn't want to bother learning PDP-11 machine language,
and DIDN'T learn to do it that way!

This one gets follow-ups to alt.folklore.computers.
--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.

browns@iccgcc.decnet.ab.com (Stan Brown) (04/13/91)

In article <4496@gmdzi.gmd.de>, wittig@gmdzi.gmd.de (Georg Wittig) writes:
> So, is
> 
> 	``c++''		always equivalent to	``(++c - 1)''

Not necessarily.  You'd have to take overflow into account, and it
might depend on whether c is signed or unsigned.

Stan Brown, Oak Road Systems, Cleveland, Ohio, USA    +1 216 371 0043
                   email (until 91/4/30): browns@iccgcc.decnet.ab.com
My opinions are mine:  I don't speak for any other person or company.

gwyn@smoke.brl.mil (Doug Gwyn) (04/13/91)

In article <3730@inews.intel.com> bhoughto@nevin.intel.com (Blair P. Houghton) writes:
>>... it is nice to express what you mean as directly as possible.
>    i += 1;	"Increment i."

++i; is in every way equivalent, including the value that is ignored
of the expression in the expression-statement.  I know of no skilled
C programmer who writes i += 1 under normal circumstances, as ++i is
more directly expressive of the notion.

This doesn't matter a whole lot, other than that students of the
language should try to adopt an idiomatic style that fits into
established practice, to improve communication with co-workers.

twpierce@amherst.bitnet (Tim Pierce) (04/15/91)

In article <1991Apr10.094402.28033@tkou02.enet.dec.com>, diamond@jit345.swstokyo.dec.com (Norman Diamond) writes:
> In article <3730@inews.intel.com> bhoughto@nevin.intel.com (Blair P. Houghton) writes:
>>    i += 1;	"Increment i."
> Nope; it's
>      i += 1;    "Increment i then discard the resulting value"
> 
> (as opposed to  j = (i += 1);  "Increment i then use the resulting value")

Look, technically, that's "Increment i, assign the resulting value to j, then
discard it."  But so what?

-- 
____ Tim Pierce                     / Eat a live toad first thing in the
\  / BITnet: twpierce@amherst       / morning, and nothing worse will
 \/  Internet: twpierce@amherst.edu / happen to you the rest of the day.

   Never underestimate the bandwidth of a station wagon filled with tapes.

mehl@iastate.edu (Mark M Mehl) (04/17/91)

>In article <1991Apr11.151933.25867@ingres.Ingres.COM> mikes@ingres.com (Mike Schilling) writes:
>>We old folks use:
>>	c++;
>>	--c;

In <1991Apr12.100933.2854@tkou02.enet.dec.com> diamond@jit345.swstokyo.dec.com (Norman Diamond) writes:
>Some of us older folks didn't want to bother learning PDP-11 machine language,
>and DIDN'T learn to do it that way!

Note that both the PDP-11 and Motorola 6809 processors do _not_ have
a pre-incrementing addressing mode or a post-decrementing addressing
mode; however, these processors do support post-inc and pre-dec
addressing modes.  As a result, it's _likely_ that:

c++ maybe much faster than ++c
and
--c maybe much faster than c--

for these two processors since they lack the required addressing
modes for the latter.  (Has anyone check this out?)

Since the modern CISC processor supports all possible permutations of
pre/post inc/decrementation addressing modes, I doubt there would be
much difference.  RISC processors (like the dual X,Y auto-indexing
performed by vector-orientated machines, e.g. DSP processors) maybe
a different story of course.

Followups back to comp.lang.c
-- 
 /\ Mark M Mehl, alias Superticker (Supertickler to some)
<><> Internet: mehl@IAstate.edu
 \/ Preferred UUCP: uunet!iastate.edu!mehl
Disclaimer: You got to be kidding; who would want to claim anything I said?

berry@arcturus.uucp (Berry;Craig D.) (04/19/91)

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

>In article <389@civet.ukc.ac.uk> mtr@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.

jeenglis@alcor.usc.edu (Joe English) (04/21/91)

berry@arcturus.uucp (Berry;Craig D.) writes:
>bhoughto@nevin.intel.com (Blair P. Houghton) writes:
>>    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?  

"Discard the resulting value" is a no-op.  It's equivalent
to "fail to use the resulting value in any subsequent calculations",
which is implicitly stated in that the value isn't used again.
I might read 

       (void)(i += 1);

as "add one to i, then discard the resulting value" but in

       i += 1;
    
there's no explicit "discard", and talking about the implicit
one doesn't really help to illustrate the semantics.


This is getting silly...


--Joe English

  jeenglis@alcor.usc.edu

bhoughto@pima.intel.com (Blair P. Houghton) (04/22/91)

In article <1991Apr18.230559.28858@arcturus.uucp> berry@arcturus.uucp (Berry;Craig D.) writes:
>My conclusion:  "++c" and "c += 1" are semantically
>equivalent, and "++c" is shorter.  My vote goes for the
>latter.

There's about six years' worth of Obfuscated C Code Contest winners
that tend to indicate that shorter is not a reason for better.

				--Blair
				  "Necessary nor sufficient."