2 Replies - 12397 Views - Last Post: 30 August 2006 - 05:34 AM Rate Topic: -----

#1 venkateshpatchala  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-August 06

Post vs. Pre increment

Posted 29 August 2006 - 11:51 PM

what is the difference between post increment and pre increment?[color=#FF0000]
Is This A Good Question/Topic? 0
  • +

Replies To: Post vs. Pre increment

#2 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Post vs. Pre increment

Posted 30 August 2006 - 02:04 AM

mmm. well here's an example:
#include<stdio.h>
int main()
{
	int a,b,c,d;
	a=1;
	b=1;
	c=++a;
	d=b++;
	printf("%d\n %d\n %d\n %d",a,b,c,d);

	return 0;
}




Now the output for this would be :
2
2
2
1

as u can see, in the case of ++a, 1st a is incremented then its value is asigned to c, in the 2nd case. 1st, b's value is assigned to d.
There's one more place which this makes a diff(there may be more. cant think of any right now :P ). Suppose:
int i = 0;
while (i++<10)
{
/*statements*/
}



and

int i = 0;
while (++i<10)
{
/*statements*/
}



in the first case, i is first compared with 10 then it is incremented. In the second case, i is 1st incremented then its compared. Hope u got it :)
Similarly u have post and pre decrement using '--'.

This post has been edited by Louisda16th: 30 August 2006 - 02:05 AM

Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Post vs. Pre increment

Posted 30 August 2006 - 05:34 AM

Great Examples, Louisda16th.

Also remember that using these operators in arguments of Macros result in compiler-dependent results.
Click here for more details on this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1