Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,373 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,157 people online right now. Registration is fast and FREE... Join Now!




Post vs. Pre increment

 
Reply to this topicStart new topic

Post vs. Pre increment

venkateshpatchala
post 29 Aug, 2006 - 10:51 PM
Post #1


New D.I.C Head

*
Joined: 29 Aug, 2006
Posts: 2


My Contributions


what is the difference between post increment and pre increment?[color=#FF0000]
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 30 Aug, 2006 - 01:04 AM
Post #2


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


mmm. well here's an example:
CODE

#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 tongue.gif ). Suppose:
CODE

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


and

CODE

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 smile.gif
Similarly u have post and pre decrement using '--'.

This post has been edited by Louisda16th: 30 Aug, 2006 - 01:05 AM
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 30 Aug, 2006 - 04:34 AM
Post #3


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

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 05:55AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month