liveBook · Manning

Increment

As explained in Modern C

Increment is the operation of increasing a numeric C object or value by one.

Increment increases a numeric object or value by one. In C, ++i is equivalent to i += 1, and the operation changes the object while also producing a value. section 4.2 section 2.1

Incrementing a loop variable

for (size_t i = 0; i < 5; ++i) { printf("element %zu\n", i); } The loop increments i after each iteration, producing successive values from 0 through 4. section 2.4

Choose the form according to which value the surrounding expression needs:

  • Prefix increment, ++i, modifies i first and returns the incremented value. section 4.2
  • Postfix increment, i++, returns the original value and performs the modification afterward; both forms leave the object incremented. section 4.2
  • Avoid combining increments with other modifications in one statement: side effects make evaluation difficult to follow. section 4.2
16   for (size_t i = 0; i < 5; ++i) {
17      printf("element %zu is %g, \tits square is %g\n",
18             i,
19             A[i],
20             A[i]*A[i]);
21   }
22
23   return EXIT_SUCCESS;


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":8}],[{\"line\":0,\"ch\":10},{\"line\":0,\"ch\":16}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":8},{\"line\":1,\"ch\":14}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":2}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":2}],[{\"line\":7,\"ch\":5},{\"line\":7,\"ch\":11}],[{\"line\":7,\"ch\":12},{\"line\":7,\"ch\":24}]]"}
!@%STYLE%@!

Inspect a complete loop showing prefix increment alongside initialization, comparison, array access, output, and return. section 2.4

How Modern C covers it

How deep it goes
Explains equivalence, prefix and postfix evaluation, and side-effect risks, while stopping short of deeper expression semantics.
Examples
Demonstrates ++i in a for loop that prints array elements and their squares.
What it assumes
Assumes basic C expressions, variables, operators, statements, and for-loop structure.
What you can do after
Apply increment through prefix or postfix syntax and recognize when side effects obscure an expression.
How it connects
Connects increment with assignment operators, postfix increment, decrement, for loops, statements, and side effects.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.