Skip to main content

GATE CSE 2016Programming in C › Functions & Parameter PassingVerified

GATE CSE 2016 PYQ: The value printed by the following program is _______. void f ( int * p , int m ) { m = m + 5 ; * p = * p +…

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); 
     }

    Answer

    30

    Approach

    Simulate the C program's parameter passing: the first argument is the address of i (call by reference via pointer), while the second argument is a copy of j (call by value). Update the pointed-to variable i and leave j unchanged, then evaluate i+j.

    Only i is modified through the pointer; j is passed by value and its local copy m is changed instead. Thus i becomes 20 and j stays 10, giving output 30.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.