edited by
8,799 views
29 votes
29 votes

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

7 Answers

Best answer
47 votes
47 votes

$i$ is called by reference and $j$ is called by value.

So, in function $f()$ only value of $i$ might change,
Now, in function $f(*p,m)$
$*p$ is pointing to $i$
Thus $*p$ is $5$.
$m$ is $10$ because of call by value of $j$.

  1. $m=10+5$ hence $m=15$
  2. $*p=5 + 15$ hence $*p=20$, that is, value of variable $i$ is now $20$
  3. returns nothing

Now, back to main
$i=20$ and $j$ is as it is $10$

Hence, output of printf will be $i+j=20+10 = 30.$

edited by
9 votes
9 votes
  void f(int * p, int m) {  // function call come here with parameter p is pointer contain i's address  and m is memory location created for m and contain j value) (4) 
           m = m + 5;  // m value increment by 5 i.e. m= 10 + 5= 15 (5)
          *p = *p + m;  // p contain address of i and *p point value at i's address so increment  that by m  i.e. *p = 5 + 15 =20 now *p point i which contain 20 (6)
           return;  // return control to main program  (7)
      }  
      void main () {   // start from here (1)
       int i=5, j=10;  // memory created for i and j with value 5, 10 store in it (2)

       f (&i, j);       // function call (address of i and value of j as parameter) (3)
       printf ("%d", i+j); // since i value changed during call by reference print 20 + 10 = 30 (8)
     } 
edited by
4 votes
4 votes
Concept:- Call by reference & Call by value

Call by reference:- The change in argument value in called function(f) reflected into calee function(main).

Call by value:- Doesn't Reflect

Here variable i is Called by reference with original Value 5 then it changed to 20

By statement *p=*p+m where *p is Value at p means  i

and argument j is passed by Value so it's change in called function (f)not reflected to calee function (main).

So Finally Ans is

i+j= 20+10=30
Answer:

Related questions