what is the difference from putting the ++ operator in c++ before and after the variable like
n++ or ++n
I know there is a difference but I don't know what it is and the books i have read don't explain it well
c++ ++ operator where to put it
Moderator: Coders of Rage
Re: c++ ++ operator where to put it
first off google is great... and wont give you snotty startups like this.
anyways.
try something like this.
that should make things abundantly clear.
anyways.
try something like this.
Code: Select all
int a = 10;
printf("%d\n", a++);
printf("%d\n", a);
int b = 10;
printf("%d\n", ++b);
printf("%d\n", b);
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: c++ ++ operator where to put it
Postfix ++ allows you to use a statement where you can take the previous value, and then increment it afterwards
Prefix increments the variable first, thus giving the variable your assigning it to, the incremented value.
e.g.
Prefix increments the variable first, thus giving the variable your assigning it to, the incremented value.
e.g.
Code: Select all
int a = 0;
int b = 5;
a = b++; // a will equal 5, b will equal 6
a = 0;
b = 5;
a = ++b ; // a will equal 6, b will equal 6
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: c++ ++ operator where to put it
Also if the statement is only "i++" or "++i", such as in a for loop, consider using ++i. It's faster on non-built-in types (non-built-in as in, not a string or an int or anything else that highlights automatically), because i++ makes a copy.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- ansatsusha_gouki
- Chaos Rift Newbie
- Posts: 37
- Joined: Fri Jul 04, 2008 1:19 am
- Location: Cleveland
- Contact:
Re: c++ ++ operator where to put it
I got confused with the whole post and prefix operator myself
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: c++ ++ operator where to put it
In general, prefix does the increment as the first thing in the statement, and postfix does the increment as the last thing in the statement.
But run avansc's sample code.
But run avansc's sample code.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.