+John Teacake MVC Posted October 15, 2004 MVC Share Posted October 15, 2004 Please could someone clearly explain the difference between the Post/Pre Increment/Decrement that is used in C quite often. For example SomeVariable++;SomeVariable--; or++SomeVariable;--SomeVariable; What is the difference exactly and do they do different things ? Thanks in Advance :) Link to comment Share on other sites More sharing options...
0 Andareed Posted October 15, 2004 Share Posted October 15, 2004 Postfix evaluates to the value before increment/decrement while prefix evaluates to the value after increment/decrement. Consider: i = 2; printf("%d\n", i++); // prints 2 // i == 3 i = 2; printf("%d\n", ++i) // prints 3 // i == 3 Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 15, 2004 Share Posted October 15, 2004 By themselves without assignment or printing, the two do exactly the same thing. With assignment, ++value increments value before assignment. value++ does it after. Link to comment Share on other sites More sharing options...
0 Sn1p3t Posted October 15, 2004 Share Posted October 15, 2004 Here's an example similar to Andareed's: int i = 0; int j = i++; // j = 0, i = 1 int i = 0; int j = ++i; // j = 1, i = 1; Link to comment Share on other sites More sharing options...
0 +John Teacake MVC Posted October 16, 2004 Author MVC Share Posted October 16, 2004 Still dont fully understand it. Link to comment Share on other sites More sharing options...
0 TCK Posted October 16, 2004 Share Posted October 16, 2004 i++ increment after evalutation it means int i=0, j=0;j=i++; j=0, i=1i is assigned before incremented int i=0, j=0;j=++i; j=1, i=1 j is incremented, then assigned to j another difference is that ++i is faster than i++ (intel compliler manual said) Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 16, 2004 Share Posted October 16, 2004 (edited) i++ increment after evalutation it meansj=0, i=1 i is assigned before incremented j=1, i=1 j is incremented, then assigned to j another difference is that ++i is faster than i++ (intel compliler manual said) 584747061[/snapback] Yeah, i++ requires it making a temporary object in memory. Pre vs. Post Increment/DecrementPrefer pre-increment and -decrement to postfix operators. Postfix operators (i++) copy the existing value to a temporary object, increment the internal value, and then return the temporary. Prefix operators (++i) increment the value and return a reference to it. With objects such as iterators, creating temporary copies is expensive compared to built-in ints. http://bdn.borland.com/article/0,1410,28278,00.html Edited October 16, 2004 by kjordan2001 Link to comment Share on other sites More sharing options...
Question
+John Teacake MVC
Please could someone clearly explain the difference between the Post/Pre Increment/Decrement that is used in C quite often. For example
orWhat is the difference exactly and do they do different things ? Thanks in Advance :)
Link to comment
Share on other sites
6 answers to this question
Recommended Posts