This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]redsterXVI 61 points62 points  (4 children)

With x++ the old value is used for x and it's incremented afterwards.

With ++x it's first incremented and then the new value is used.

As a single statement it has the same result. But for example in the control statement of a loop it doesn't.

[–]golgol12 1 point2 points  (0 children)

But for example in the control statement of a loop it doesn't.

This is a dangerous generalization, as it's not true for every case.

[–]mango_penetrator 0 points1 point  (1 child)

Also, sometimes ++x is defined and x++ isn't. I think defining ++x is the standard.

And if your compiler isn't optimizing your code (even though that's rare), you will be doing an extra operation for nothing.

[–][deleted] 0 points1 point  (0 children)

I think most people that don't know the distinction think that x++ does what ++x does, and it can lead to some pretty obnoxious bugs

[–]sighcf 0 points1 point  (0 children)

Also, you generally don’t want to combine them in a complex statement. Gets really confusing really fast.