ProgrammingModerateTCS NQT · Infosys

What is printed?

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

Correct answer: A. 2

p moves to a[2] = 3, and p - 2 points at a[0] = 1, so the difference is 2. Pointer arithmetic is in elements, not bytes.

More C Programming questions

Exams that ask this topic