C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 10)
10.
What will be the output of the program?
#include<stdio.h>
int fun(int, int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
    printf("%d\n", proc(fun, 6, 6));
    return 0;
}
int fun(int a, int b)
{
   return (a==b);
}
int proc(pf p, int a, int b)
{
   return ((*p)(a, b));
}
6
1
0
-1
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
49 comments Page 1 of 5.

Raj said:   3 years ago
Thanks @Vishwas.

Leonardo said:   4 years ago
Can anybody explain the Typedef line what is the meaning of typedef int (*pf) (int, int); and Pf p what the meaning of p here thanks in advance.
(1)

Mohamed said:   5 years ago
typedef int (*pf) (int, int); //pf is apointer to function which holds the address of function int which returns two integer value,printf("%d\n",proc(fun,6,6)) // when calling function proc ,it returns 1 because a=b=6.

p=&pf=&fun(a,b)=&1,so *p=1;
(3)

Sumit Tripathi said:   6 years ago
In main the first line

printf("%d\n",proc(fun,6,6));
is calling proc which is taking argument a function pointer and two integer values. Function pointer pf is defined as typedef int(*pf) (int,int); This line printf("%d\n",proc(fun,6,6)); will call the function defined as:

int proc(pf p,int a,int b){
return ((*p)(a,b));
}
Now in this function pf holds the pointer to function fun. This will cause the function fun to be called which is returning whether the values of a and b are true or not. Since you have passed 6,6 as the arguments the result will be true and that is why you are getting as 1 as an Answer.
(3)

Suraj said:   6 years ago
Thanks @Vishwas.

Ananthesh said:   6 years ago
int fun(int,int);
Function takes 2 int arguments and returns an int.

typedef int(*pf) (int,int);
pf is a function pointer that store the address of address of a function which takes two ints as its agrs and returns an int.

int proc(pf,int,int);
proc is a function which takes 3 args first is a function pointer to a function like above and two integer args.

proc(fun,6,6);
Above statement calls fun with two args 6 and 6 and returns true if they are equal which is how the result is 1.

Dhivya said:   7 years ago
Here, proc is indirectly calling fun via a function pointer. The arguments that fun receives are again 6 and 6, and the equality operator evaluates to an int with the value 1 because they are equal. If they were not equal, the == operator would yield 0.

Manish Thakur said:   7 years ago
== operator upon aucces it will return 1 so one is printed.

Himanshu said:   7 years ago
HINTS : consider printf in main takes Number of char,

Step 1 : proc(fun, 6, 6) in printf of main call int proc(pf p, int a, int b)
Step 2: return ((*p)(a, b)); call int fun(int a, int b).
Step 3: return (a==b); //here a=6;b=6; a==b means 6==6 condition is ture then 1;
return 1;
Step4 : Now return is 1.
(1)

Zahed said:   7 years ago
Thanks all.


Post your comments here:

Your comments will be displayed after verification.