GATE Computer ScienceVery hardGATE CS (PYQ pattern)

void f(int *x, int *y, int z) { *x = *y + z; *y = *x - z; z = *x; } int main() { int a = 1, b = 2, c = 3; f(&a, &b, c); f(&b, &a, c); f(&a, &a, c); printf("%d %d %d", a, b, c); } What is printed?

  1. A. 5 8 3
  2. B. 5 8 8
  3. C. 8 8 3
  4. D. 5 5 3

Correct answer: A. 5 8 3

f(&a,&b,3): a = 2+3 = 5, b = 5-3 = 2. f(&b,&a,3): b = 5+3 = 8, a = 8-3 = 5. f(&a,&a,3) aliases both pointers: a = 5+3 = 8, then a = 8-3 = 5. c stays 3, being passed by value.

More Programming and Data Structures (GATE) questions

Exams that ask this topic