c++ ++ operator where to put it

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

c++ ++ operator where to put it

Post by herby490 »

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
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: c++ ++ operator where to put it

Post by avansc »

first off google is great... and wont give you snotty startups like this.

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);
that should make things abundantly clear.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
programmerinprogress
Chaos Rift Devotee
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

Post by programmerinprogress »

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.

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
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: c++ ++ operator where to put it

Post by MarauderIIC »

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.
User avatar
ansatsusha_gouki
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 37
Joined: Fri Jul 04, 2008 1:19 am
Location: Cleveland
Contact:

Re: c++ ++ operator where to put it

Post by ansatsusha_gouki »

I got confused with the whole post and prefix operator myself :roll:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: c++ ++ operator where to put it

Post by MarauderIIC »

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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply