StreakPeaked· Practice

ExamsGATETechnical

What does the following program print? ```c #include <stdio.h> void f(int *p, int *q){ p = q; *p = 2; } int i = 0, j = 1; int main(){ f(&i, &j); printf("%d %d\n", i, j); return 0; } ```

  1. 2 2
  2. 2 1
  3. 0 1
  4. 0 2

Correct answer: 0 2

Solution

In the function, `p = q` makes the local pointer `p` point to `j`. Then `*p = 2` updates `j` to 2. Variable `i` remains unchanged because only the local copy of `p` was reassigned.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →