ProgrammingModerateAccenture · Capgemini pattern

What is printed?

What is printed?
int i, j;
for (i = 0, j = 0; i < 5; i++, j += i);
printf("%d %d", i, j);
  1. A. 5 10
  2. B. 4 10
  3. C. 5 14
  4. D. 5 15

Correct answer: D. 5 15

The ; right after the for header is an empty body. The comma operator evaluates left to right, so each step first increments i and then adds the NEW i to j: 1 + 2 + 3 + 4 + 5 = 15. The loop stops with i = 5. 10 would add i before incrementing.

More C Programming questions

Exams that ask this topic