ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int i;
    int a[5] = {0};
    for (i = 0; i < 5; i++)
        a[i] = i * i;
    printf("%d", a[4] - a[2]);
}
  1. A. 8
  2. B. 6
  3. C. 12
  4. D. 16

Correct answer: C. 12

a[4] = 16 and a[2] = 4, so the difference is 12. Index 4 is the last valid element of a five-element array.

More C Programming questions

Exams that ask this topic