Prefix vs postfix increment in C++

What's the difference between the prefix and postfix increment/decrement operators in C++? When should I use one over the other?

In C++, the increment (++) and decrement (--) operators come in two forms: prefix and postfix. They both increment or decrement the variable, but they differ in when this incrementing/decrementing takes effect.

  1. Prefix increment/decrement (++x, -x): Increments/decrements the value of x, and then returns the new value of x.
  2. Postfix increment/decrement (x++, x--): Returns the current value of x, and then increments/decrements x.

Here's an example that demonstrates the difference:

#include <iostream>

int main() {
  int x = 5;
  int y = ++x;  // y is 6, x is 6
  int z = x++;  // z is 6, x is 7

  // Outputs "7 6 6"
  std::cout << x << ' ' << y << ' ' << z;  
}
7 6 6

In the first case, x is incremented before its value is assigned to y, so both x and y end up as 6.

In the second case, the current value of x (which is 6) is assigned to z, and then x is incremented. So z ends up as 6 and x ends up as 7.

As for when to use one over the other, it largely depends on your specific needs. If you don't need the original value, the prefix version is usually preferred as it can be slightly more efficient. The postfix version is useful when you need to use the original value for something else in the same expression.

However, in most cases, it's best to increment/decrement in a separate statement to avoid confusion and potential bugs. The difference between prefix and postfix is most important when the increment/decrement is part of a larger expression.

Variables, Types and Operators

Learn the fundamentals of C++ programming: declaring variables, using built-in data types, and performing operations with operators

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

How does integer division work in C++?
Can you explain more about how integer division and the modulus operator work in C++, with some examples?
Implicit casts between booleans and numbers in C++
The lesson mentions that numeric types can be implicitly cast to booleans in C++, with 0 being false and anything else true. Can you clarify how that works with an example?
What is uniform initialization in C++?
The lesson recommends using uniform initialization with braces (like int x {5};) instead of the = operator. What advantages does this have?
When should I use auto in C++?
The auto keyword seems convenient, but the lesson says to use it sparingly. When is it appropriate to use auto in C++?
How does operator precedence work in C++?
Can you clarify the rules around operator precedence in C++? How can I control the order of operations?
Floating point precision in C++
I've heard that floating point numbers can sometimes be imprecise in C++. Can you explain why this happens and how to handle it?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant