Sobes.tech
Junior — Middle

What are the differences between prefix and postfix increment operations on a variable?

sobes.tech AI

Answer from AI

In C/C++, the prefix (++i) and postfix (i++) increment operations differ in execution order and return value:

  • Prefix increment (++i): first increases the variable's value by 1, then returns the increased value.
  • Postfix increment (i++): first returns the current value of the variable, then increases it by 1.

Example:

int i = 5;
int a = ++i; // i becomes 6, a = 6

int j = 5;
int b = j++; // b = 5, j becomes 6

This is important to consider when using in expressions to avoid unexpected results.