Home » C Programming » Functions » Question
  1. Which of the following is the correct output for the program given below?
    #include <stdio.h>
    void fun ( int*, int* ) ;
    int main( )
    {
    int a = 6, b = 3 ;
    fun (&a, &b);
    printf ("%d %d\n" , a, b);
    return 0 ;
    }
    void fun (int*a, int*b)
    {
    *a = *a* *a;
    *b = *b* *b ;
    }
    1. 6 3
    2. 12 6
    3. 3 6
    4. 36 9
    5. 9 36
Correct Option: D

Step 1: int a=6, b=3; Here variable a and b are declared as an integer type and initialized to 6 and 3 respectively.

Step 2: fun(&a, &b); Here the function fun() is called with two parameters &a and &b (The & denotes call by reference. So the address of the variable a and b are passed. )

Step 3: void fun(int *a, int *b) This function is called by reference, so we have to use * before the parameters.

Step 4: *a = *a**a; Here *a denotes the value of the variable a. We are multiplying 5*5 and storing the result 25 in same variable a.

Step 5: *b = *b**b; Here *b denotes the value of the variable b. We are multiplying 2*2 and storing the result 4 in same variable b.

Step 6: Then the function void fun(int *a, int *b) return back the control back to main() function.

Step 7: printf("%d, %d", a, b); It prints the value of variable a and b.

Hence the output is 36, 9.



Your comments will be displayed only after manual approval.