ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int main(void) {
    int a = 10;
    int *p = &a;
    int **pp = &p;
    printf("%d %d %d", a, *p, **pp);
    a = 20;
    printf(" %d %d %d", a, *p, **pp);
}
  1. A. 20 20 20 20 20 20
  2. B. 10 20 10 20 20 20
  3. C. 10 10 10 20 20 20
  4. D. 10 10 10 10 10 10

Correct answer: C. 10 10 10 20 20 20

p and pp both ultimately refer back to a; any change to a is immediately visible through every level of indirection, since they're not copies but references to the same memory: before the change all three read 10, after `a=20` all three read 20.

More C Programming questions

Exams that ask this topic