i++ and ++i

Reply

Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: i++ and ++i

 
0
  #21
Oct 1st, 2007
He's referring to the way you write them when you wish to overload the operators.
http://www.parashift.com/c++-faq-lit...html#faq-13.14

However,
1. He's 3 years too late with the information.
2. It's off topic as the thread was about how the prefix and postfix forms affect integers, and not about how to write overload versions.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 4
Reputation: prabhat padhy is an unknown quantity at this point 
Solved Threads: 0
prabhat padhy prabhat padhy is offline Offline
Newbie Poster

Re: i++ and ++i

 
0
  #22
Oct 1st, 2007
Originally Posted by Tekmaven™ View Post
//Lets declare i, and set it equal to 4.
int i = 4;

//Now say their is a function nammed goFetch(int), and we wanted to pass an increment of i to it

//i = 4 before this code gets touched, the value 4 would be passed, and then i would become 5
goFetch(i++);

//i = 4 before this code gets touched, i gets incrimented before anything else (and becomes 5), and the value 5 would be passed
goFetch(++i);

So, ++i means to incriment first then give the incrimented value, and i++ means to give the original value, then incriment.


Hi,

As i++ is used in a statement, then the valude of i in the statement, remains same ie unincremented. But after the statement, this is incremented.

When ++i, is used then it is incremented with in the statement it self.

bye
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1
Reputation: shabinesh is an unknown quantity at this point 
Solved Threads: 0
shabinesh shabinesh is offline Offline
Newbie Poster

Re: i++ and ++i

 
0
  #23
Oct 1st, 2007
hi
when inside a loop for example
for(i=0;i<10;i++);
here the loop starts from 0,
and in the case of ++i;
the loop starts from 1,
in the former the assignment is before increment
in the later the assignment is after i gets incremented
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: i++ and ++i

 
0
  #24
Oct 2nd, 2007
>>and in the case of ++i;
>>the loop starts from 1,

No it doesn't -- the loop still starts with 0 because the i is not incremented until after the iteration of that loop finishes.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: Gopala Krishnan is an unknown quantity at this point 
Solved Threads: 0
Gopala Krishnan Gopala Krishnan is offline Offline
Newbie Poster

Re: i++ and ++i

 
0
  #25
Oct 7th, 2007
i++ first takes the value of i and then gives the increment----(POST INCREMENT OPERATOR) whereas ++i first gives the increment to the assigned i value---(PRE INCREMENT OPERATOR)

example::: i=6

i++ takes i and then gives increment i.e.,i++=5 but the compiler stores the value as 6 which is not the displayed output

++i gives increment first i.e., ++i=6...

  1. void main()
  2. {
  3. int i,x,y;
  4. printf("enter an int:");
  5. scanf("%d",&i);
  6. x=i++;
  7. y=++i;
  8. printf("%d\n%d",x,y);
  9. getch();
  10. }
input: enter an int:5
output:5
7

HERE THE VALUE OF ++i IS 7 COZ THE COMPILER STORES THE VALUE OF i++ AS 6...
Last edited by Ancient Dragon; Oct 7th, 2007 at 7:14 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: i++ and ++i

 
0
  #26
Oct 7th, 2007
WTF.. All she asked was the difference between ++i and i++, not such weird details. Can't you just say that, ++i will first increment the value of i and then assign it., while i++ will first assign the value and then increments (so the variable assigned before gets to keep the old value of i).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: i++ and ++i

 
0
  #27
Oct 7th, 2007
Originally Posted by ChaseVoid View Post
WTF.. All she asked was the difference between ++i and i++, not such weird details.
I agree (beating a dead horse)-- but everyone has to get in his/here 2 cents worth
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: i++ and ++i

 
0
  #28
Oct 7th, 2007
xDD Totally, kekeke.. Wonder what they'll throw at us if we ask the difference between a long int and a short int. kekeke
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: i++ and ++i

 
0
  #29
Oct 7th, 2007
Don't mean to hijack this thread, but yes there may be a difference, but then again there might not be one also. depends on the compiler, which I'm sure you already know but other readers may not.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: i++ and ++i

 
0
  #30
Oct 7th, 2007
Hehe, xD
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC