Xref: utzoo comp.lang.c:38082 comp.std.c:4640 Newsgroups: comp.lang.c,comp.std.c Path: utzoo!utgpu!cunews!bnrgate!bwdls61!bwdla28!coop4y44 From: coop4y44@bwdla28.bnr.ca (Takis Skagos) Subject: Re: difference between c++; and ++c; Message-ID: <1991Apr9.125207.22511@bwdls61.bnr.ca> Summary: Diff's between ++c and c++ Keywords: pre post increment decrement grammar style Sender: usenet@bwdls61.bnr.ca (Use Net) Organization: Bell-Northern Research, Ottawa, Canada References: <1991Apr08.161444.10025@cs.ruu.nl> <1817@bbxsda.UUCP> Date: Tue, 9 Apr 1991 12:52:07 GMT 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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-