ProgrammingEasyTCS NQT · Infosys pattern

What is printed?

What is printed?
int a[] = {4, 8, 15, 16, 23};
printf("%zu %d", sizeof(a) / sizeof(a[0]), a[sizeof(a) / sizeof(a[0]) - 1]);
  1. A. 20 23
  2. B. 5 16
  3. C. 5 23
  4. D. 4 23

Correct answer: C. 5 23

sizeof(a) is the size of the whole array (5 × sizeof(int)) and sizeof(a[0]) is one element, so the quotient is the element count, 5, whatever the size of int. The last valid index is 5 − 1 = 4, which holds 23. 20 is sizeof(a) itself with 4-byte int.

More C Programming questions

Exams that ask this topic