Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing in C++ - When to use x++ or ++x?

I'm currently learning C++ and I've learned about the incrementation a while ago. I know that you can use "++x" to make the incrementation before and "x++" to do it after.

Still, I really don't know when to use either of the two... I've never really used "++x" and things always worked fine so far - so, when should I use it?

Example: In a for loop, when is it preferable to use "++x"?

Also, could someone explain exactly how the different incrementations (or decrementations) work? I would really appreciate it.

like image 491
Jesse Emond Avatar asked Nov 28 '09 16:11

Jesse Emond


People also ask

What is difference between ++ x and x ++ in C?

What is the difference between x++ and ++x? X++ is post increment and ++x is pre increment. X++ value is incremented after value assign or printed.

Which is faster x ++ or ++ x?

The truth of the matter is that *x++ was faster than *++x in C on a PDP-11, and maybe a VAX, because it compiled to a single instruction (autoincrement register deferred).

What does ++ x mean in C?

Syntax: a = ++x; Here, if the value of 'x' is 10 then the value of 'a' will be 11 because the value of 'x' gets modified before using it in the expression.

Whats the difference between x ++ and ++ x?

The only difference between the two is their return value. The former increments ( ++ ) first, then returns the value of x , thus ++x . The latter returns the value of x first, then increments ( ++ ), thus x++ .


2 Answers

It's not a question of preference, but of logic.

x++ increments the value of variable x after processing the current statement.

++x increments the value of variable x before processing the current statement.

So just decide on the logic you write.

x += ++i will increment i and add i+1 to x. x += i++ will add i to x, then increment i.

like image 95
Oliver Friedrich Avatar answered Oct 21 '22 18:10

Oliver Friedrich


Scott Meyers tells you to prefer prefix except on those occasions where logic would dictate that postfix is appropriate.

"More Effective C++" item #6 - that's sufficient authority for me.

For those who don't own the book, here are the pertinent quotes. From page 32:

From your days as a C programmer, you may recall that the prefix form of the increment operator is sometimes called "increment and fetch", while the postfix form is often known as "fetch and increment." The two phrases are important to remember, because they all but act as formal specifications...

And on page 34:

If you're the kind who worries about efficiency, you probably broke into a sweat when you first saw the postfix increment function. That function has to create a temporary object for its return value and the implementation above also creates an explicit temporary object that has to be constructed and destructed. The prefix increment function has no such temporaries...

like image 37
duffymo Avatar answered Oct 21 '22 17:10

duffymo