edited by
14,256 views
47 47 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); 
     }

8 Answers

Best answer
66 66 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
10 10 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
5 5 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
0 0 votes


P is a pointer stores the address of i, & m is the formal parameter of j.
Now, m = m + 5;
*p = *p + m;

Hence, i + j will be 20 + 10 = 30.

Answer:
Position:
Show:

Related questions

70 70 votes
10 answers 10 answers
22.5k
22.5k views
Akash Kanase asked Feb 12, 2016
22,487 views
Consider the following program:int f (int * p, int n) { if (n <= 1) return 0; else return max (f (p+1, n-1), p[0] - p ); } int main () { int a[] = {3, 5, 2, 6, 4}; ...
95 95 votes
5 answers 5 answers
30.4k
30.4k views
Sandeep Singh asked Feb 12, 2016
30,370 views
Consider the following C program.# include <stdio.h void mystery (int *ptra, int *ptrb) { int *temp; temp = ptrb; ptrb =ptra; ptra = temp; } int main () { int a...
141 141 votes
15 answers 15 answers
39.9k
39.9k views
Akash Kanase asked Feb 12, 2016
39,872 views
Suppose the functions $F$ and $G$ can be computed in $5$ and $3$ nanoseconds by functional units $U_{F}$ and $U_{G}$, respectively. Given two instances of $U_{F}$ and two...
35 35 votes
2 answers 2 answers
14.3k
14.3k views
Akash Kanase asked Feb 12, 2016
14,329 views
Consider the following processes, with the arrival time and the length of the CPU burst given in milliseconds. The scheduling algorithm used is preemptive shortest remain...