A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
i++ increments i and returns the old value. ++i increments i and returns the new value. In a standalone statement, they are identical. In an expression, they differ. The difference is the value the expression evaluates to. This matters in loop headers, array indexing, function arguments, and any context where the increment's return value is used. It also matters in C++ for non-trivial types where the postfix version creates a temporary copy, but in C with primitive types, the performance difference is zero at any optimization level.
#include <stdio.h>int main(void) {int a = 5;int b = 5;int result_post = a++;int result_pre = ++b;printf("Postfix: a=%d, result=%d\n", a, result_post);printf("Prefix: b=%d, result=%d\n", b, result_pre);return 0;}
Both a and b end at 6. The difference is what the expression returned. a++ gave 5 (the value before increment). ++b gave 6 (the value after increment). The increment happened in both cases. The return value is the distinction.
#include <stdio.h>int main(void) {int arr[] = {10, 20, 30, 40, 50};int i = 0;printf("Postfix in index: arr[i++] = %d\n", arr[i++]);printf("i is now: %d\n", i);i = 0;printf("Prefix in index: arr[++i] = %d\n", arr[++i]);printf("i is now: %d\n", i);return 0;}
arr[i++] accesses arr[0] then increments i to 1. arr[++i] increments i to 1 then accesses arr[1]. The index used is different. In a loop header for (int i = 0; i < n; i++), the return value of i++ is discarded, so i++ and ++i are identical. The compiler generates the same code. Use whichever reads more naturally in context.
Never write i = i++ or arr[i++] = arr[++i]. Modifying a variable more than once between sequence points is undefined behavior. The compiler can produce any result. The code is not clever. It is broken.
The increment operators are simple in isolation and treacherous in combination. The rule: use them in standalone statements and loop headers. Avoid embedding them in larger expressions where the evaluation order is unclear. Next: the ternary operator, C's inline conditional.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.