edited by
19,022 views
56 56 votes

The output of the following C program is_____________.

void f1 ( int a, int b)  {  
                int c; 
                c = a; a = b;
                 b = c;  
}   
void f2 ( int * a, int * b) {   
               int c; 
               c = * a; *a = *b; *b = c; 
} 
int main () { 
        int a = 4, b = 5, c = 6; 
        f1 ( a, b); 
        f2 (&b, &c); 
        printf ("%d", c - a - b);  
 }

5 Answers

Best answer
46 46 votes
void f1 ( int a, int b)  {    //This code is call by value
// hence no effect of actual values when run.
                int c; 
                c = a; 
                a = b;
                b = c;  
}   
void f2 ( int * a, int * b) {   //*a= address of b 
//and *b = address of c
               int c;            //int c = garbage 
               c = * a;          //c = value at address a = 5;
               *a = *b;          //*a = Exchange original
// variable value of c to b = b= 6
               *b = c;             //*b = c = 5;
} 
int main () { 
        int a = 4, b = 5, c = 6; 
        f1 ( a, b);  This has no effect on actual values
// of a ,b since Call by value.
        f2 (&b, &c); Here change will be happen.
        At this point  int a = 4, b = 6, c = 5;
        printf ("%d", c - a - b);    = (5-4)-6 = 1-6 = -5
 }
selected by
43 43 votes
Here, $f1$ will not change any values bcz it is call by value but $f2$ is call by reference and it swaps values of $b$ and $c$ and changes are also reflected in main function. So, $5-4-6= -5$ is the answer.
edited by
5 5 votes
Function $f1$ is for swapping but as parameters are passed by values so swapping of values will be done on former parameter (parameter local to $f1$) only not on actual parameter. So basically $f1$ is doing nothing here.

Function $f2$ is swapping values of $\text{b & c }$. As parameter are passed by reference ( addresses of actual variables are passed as parameter) so changes will reflect in actual variables also.
edited by
Answer:
Position:
Show:

Related questions

174 174 votes
9 answers 9 answers
47.4k
47.4k views
Misbah Ghaya asked Feb 13, 2015
47,401 views
What is the output of the following C code? Assume that the address of $x$ is $2000$ (in decimal) and an integer requires four bytes of memory.int main () { unsigned int ...
88 88 votes
15 answers 15 answers
30.6k
30.6k views
Misbah Ghaya asked Feb 13, 2015
30,572 views
Consider the DFAs $M$ and $N$ given above. The number of states in a minimal DFA that accept the language $L(M) \cap L(N)$ is_____________.
42 42 votes
3 answers 3 answers
17.3k
17.3k views
Misbah Ghaya asked Feb 12, 2015
17,284 views
Consider a system with byte-addressable memory, $32\text{-bit}$ logical addresses, $4\;\text{kilobyte}$ page size and page table entries of $4\;\text{bytes}$ each. The si...
74 74 votes
8 answers 8 answers
25.9k
25.9k views
Misbah Ghaya asked Feb 13, 2015
25,874 views
Consider the following pseudo code, where $x$ and $y$ are positive integers.begin q := 0 r := x while r ≥ y do begin r := r - y q := q + 1 end endThe post condition that ...