Increment Operators in C

Level 271 keystrokes

Shows the difference between post-increment and pre-increment: x++ prints 5 and then raises x to 6, while ++x raises x to 7 first and prints 7. The output is 5, 6 and 7 on separate lines.

C code

// x starts at 5
1int x = 5;
// Post-increment: x++ passes the old value 5 to printf, then x becomes 6
2printf("%d\n", x++);
// Prints 6, showing that the previous line did increase x
3printf("%d\n", x);
// Pre-increment: ++x raises x to 7 first, then passes 7 to printf
4printf("%d\n", ++x);

Output

5
6
7
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • "× 6
  • ;× 4
  • +× 4
  • ,× 3
  • (× 3
  • )× 3
  • \× 3
  • %× 3
  • =× 1

More C examples at level 2

← C typing practice