difference between c++; and ++c;

Takis Skagos coop4y44 at bwdla28.bnr.ca
Tue Apr 9 22:52:07 AEST 1991


In article <1991Apr08.161444.10025 at cs.ruu.nl> hnridder at 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 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



More information about the Comp.std.c mailing list