Skip to content
Official PYQ · 2016

GATE CS 2016 Set 2 Q12 — Programming in C

Explain with AI

The value printed by the following program is _______.

      void f (int * p, int m) { 
           m = m + 5; 
          *p = *p + m; 
           return; 
      }  
      void main () { 
       int i=5, j=10; 

       f (&i, j); 
       printf ("%d", i+j); 
     }
View worked solution

Answer: 30

Let us trace the execution of the program step-by-step:

  1. In main(), the variables are initialized as:
    i = 5, j = 10
  2. The function call f(&i, j) is made:
    • The pointer p receives the address of i (pass-by-reference), so *p refers directly to i.
    • The variable m receives a copy of the value of j (pass-by-value), so m = 10.
  3. Inside the function f():
    • m = m + 5   ⟹  m=10+5=15\implies m = 10 + 5 = 15.
    • *p = *p + m   ⟹  ∗p=5+15=20\implies *p = 5 + 15 = 20. Since p points to i, the value of i becomes 2020.
  4. After returning to main():
    • The value of i is updated to 2020.
    • The value of j remains unchanged at 1010 because it was passed by value.
  5. Finally, printf("%d", i+j) prints the value of i+j=20+10=30i + j = 20 + 10 = 30.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions