What the increment operator does
The increment operator, written ++, adds 1 to a variable. It is one of the unary operators in c, so it works on a single operand: the variable you want to change. The name C++ is a nod to this operator, since it reads as “C plus one.”
C++ has two forms:
- Prefix (
++count) adds 1 and then gives back the new value. - Postfix (
count++) gives back the old value and then adds 1.
When the result is not used, as in a line on its own, both forms do the same thing.
A basic example
#include <iostream>
int main() {
int count = 5;
++count; // count is now 6
count++; // count is now 7
std::cout << count << "\n"; // prints 7
return 0;
}
Prefix and postfix in expressions
When the increment is part of a larger expression, the form you pick changes the result:
#include <iostream>
int main() {
int a = 5;
int b = a++; // b gets 5, then a becomes 6
std::cout << "a = " << a << ", b = " << b << "\n";
int c = ++a; // a becomes 7, then c gets 7
std::cout << "a = " << a << ", c = " << c << "\n";
return 0;
}
The program prints:
a = 6, b = 5
a = 7, c = 7
To add a different amount, write count += 5. That form is one of the c assignment operators.
Using the increment operator in loops
A for loop is the most common place to see ++:
#include <iostream>
int main() {
for (int i = 0; i < 3; ++i) {
std::cout << i << "\n";
}
return 0;
}
This prints 0, 1, and 2 on separate lines. The same counting idea works in a while loop, where you write the update yourself. For the full layout of those loops, see c while loop syntax.
Indexes and pointers
Postfix is handy when you need the current value and then want to move on. In this example, the old value of i is used as an index, and then i moves to the next position:
#include <iostream>
int main() {
int values[3] = {10, 20, 30};
int i = 0;
int first = values[i++]; // first gets 10, then i becomes 1
std::cout << first << ", next index " << i << "\n";
return 0;
}
The output is 10, next index 1.
On a pointer into an array, ++ moves the pointer to the next element, not the next byte.
Increment for your own types
Classes can define their own ++ through operator overloading in c, and many iterator types work this way. A class defines the prefix and postfix forms as two separate functions. The postfix version takes a dummy int parameter so that C++ can tell the two apart. It also usually has to save the old state before it changes anything, so it does more work than the prefix version.
Mistakes to avoid
- Changing the same variable twice in one expression. Code such as
i++ + i++or++i + ++ihas undefined behavior in C++. - Expecting postfix to give the new value. After
int y = x++;,yis one less thanx. - Stepping past the end of an array. Moving an index past the last element and then reading from it has undefined behavior.
Key takeaways
++adds 1 to a variable.- Prefix gives back the new value, and postfix gives back the old value.
- The operator is common in loops and index updates. Avoid changing the same variable twice in one expression.