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 3 of 5.

Hema said:   8 years ago
@Vishwas.

Thanks for your explanation.

Zahed said:   7 years ago
Thanks all.

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

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.

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.

Suraj said:   6 years ago
Thanks @Vishwas.

Rita said:   1 decade ago
Thanks aashish.

Sundar said:   2 decades ago
@Preethi

It is just a user defined function.

Function Prototype:

int proc(pf, int, int)

Function Defintion:

int proc(pf p, int a, int b)
{
return ((*p)(a, b));
}


Have a nice day!

Preethi said:   2 decades ago
Thanks Sundar.

Swathi said:   2 decades ago
Please. Can any one clearly explain this program?.


Post your comments here:

Your comments will be displayed after verification.