C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 2)
2.
What will be the output of the program?
#include<stdio.h>
void fun(int*, int*);
int main()
{
    int i=5, j=2;
    fun(&i, &j);
    printf("%d, %d", i, j);
    return 0;
}
void fun(int *i, int *j)
{
    *i = *i**i;
    *j = *j**j;
}
5, 2
10, 4
2, 5
25, 4
Answer: Option
Explanation:

Step 1: int i=5, j=2; Here variable i and j are declared as an integer type and initialized to 5 and 2 respectively.

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

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

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

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

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

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

Hence the output is 25, 4.

Discussion:
24 comments Page 2 of 3.

Saumya said:   1 decade ago
#include<stdio.h>
int main()
{
float a=13.5;
float *b,*c;
b=&a;
c=b;
printf("\n%u %u %u", &a, b,c);
printf("\n%f %f %f %f %f %f", a,*(&a),*&a, *b,*c);

return 0;
}

In this problem, what would be printed because of *c i.e last argument of second printf statement.
Can anybody help me out in this?

Thirupal said:   1 decade ago
In this question the values pass call by reference so the call function and pass the values and it stores values in the function.

Vishal said:   1 decade ago
Pointer multiplication is not allowed. Then how is it possible?

Karthik said:   1 decade ago
Even though return is not used in called function, how it is returning value to main function.

Pawankumar said:   1 decade ago
Here there is no return statement then how we can use the updated value of I and j ?

Rasheeda said:   1 decade ago
i and j values will change even there is no return statement. How ?

Ruby said:   1 decade ago
While declaring the function its is necessary to inform the compiler about that function, so that "void fun(int*, int*);" is written before main() function. Otherwise it will produce a compilation error.

Ankita said:   1 decade ago
Please explain.

What is the purpose of "void fun (int*, int*);" ?

Jhanisi ri said:   1 decade ago
What is the call by reference? please explain me.

Lakshmisravani said:   1 decade ago
We can use *i and *j as arguments
so that when we calling the function i and j values are entered into formal arguments *i and *j
so,
*i**i=25
*j**j=4


Post your comments here:

Your comments will be displayed after verification.