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
