[comp.std.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

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)

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

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

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"

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.

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.