ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[4] = {10, 20, 30, 40};
    int *p = a, *end = a + 4;
    int sum = 0;
    while (p != end) { sum += *p; p++; }
    printf("%d", sum);
}
  1. A. 90
  2. B. 60
  3. C. 100
  4. D. 0

Correct answer: C. 100

The loop walks p from a to one-past-the-end, adding each element: 10+20+30+40=100.

More C Programming questions

Exams that ask this topic