ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
int a[3] = {5,10,15};
int *p = a;
int *q = &a[2];
while (p != q) {
    printf("%d ", *p);
    p++;
}
printf("%d", *p);
  1. A. 5 10 5
  2. B. 5 15
  3. C. 5 10 15 15
  4. D. 5 10 15

Correct answer: D. 5 10 15

The loop prints while p hasn't reached a[2]: it prints a[0]=5 then a[1]=10, then p becomes &a[2] and the loop stops. The final printf outside the loop then prints *p = a[2] = 15. Output: 5 10 15.

More C Programming questions

Exams that ask this topic