Home / C Programming / Functions :: Discussion

Discussion :: Functions

  1. What will be the output of the program?

    #include 
     int fun(int i) 
     {
        
        i++;    
        return i; 
     }  
     
     int main() 
     {     
        int fun(int);    
        int i=3;    
        fun(i=fun(fun(i)));    
        printf("%d\n", i);    
        return 0; 
    } 

     

     

  2. A.

    5

    B.

    4

    C.

    Error

    D.

    Garbage value

    View Answer

    Workspace

    Answer : Option A

    Explanation :

    Step 1: int fun(int); This is prototype of function fun(). It tells the compiler that the function fun() accept one integer parameter and returns an integer value.

    Step 2: int i=3; The variable i is declared as an integer type and initialized to value 3.

    Step 3: fun(i=fun(fun(i)));. The function fun(i) increements the value of i by 1(one) and return it.

    Lets go step by step,

    => fun(i) becomes fun(3) is called and it returns 4.

    => i = fun(fun(i)) becomes i = fun(4) is called and it returns 5 and stored in variable i.(i=5)

    => fun(i=fun(fun(i))); becomes fun(5); is called and it return 6 and nowhere the return value is stored.

    Step 4: printf("%d\n", i); It prints the value of variable i.(5)

    Hence the output is '5'.


Be The First To Comment