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

Pravat said:   1 decade ago
No one is mentioning about pointer function.

#include<stdio.h>
int fun(int, int);
typedef int (*pf) (int, int);//Here pf is a pointer of type function.

/*It means pf could point to any function that would a return value of int and would accept 2 args of type int*/

int proc(pf, int, int);

int main()
{
printf("%d\n", proc(fun, 6, 6));

/*Here the address of fun is passed to the called function proc*/

return 0;
}
int fun(int a, int b)
{
return (a==b);//return(1)

//As a==b it will return a bool value i.e. true value=1.

}
int proc(pf p, int a, int b)

/*Now proc args are pf p i.e.a pointer to function .it means pf has the address of fun and now it will call the fun function*/
{
return ((*p)(a, b));
}

XYZ said:   1 decade ago
How this happened?

Thenu said:   9 years ago
@Vishwas.

Thanks, Clearly understood.

Azagumozhi said:   9 years ago
Nice answer @Vishwas.

Ashwini Gour said:   9 years ago
Thanks, @Manukundloo.

Ashish Patil said:   9 years ago
Thanks to all of your explanation.

Mompp said:   9 years ago
Basically function return to the operating system either 0 or 1.

Here (a==b) means return 1 and now,

(1,6,6) all are non-zero values so the output will be 1(a non-zero number)

I hope you all understand.

Saroj said:   8 years ago
Please, anyone explain me clearly.

Aadarsh said:   8 years ago
First statement of main - printf("%d\n" ,proc(fun,6,6);

Calls the function int proc(pf p, int a, int b). This function returns to int fun(int a, int b).the return statement here a==b is true since a=b=6.Hence it returns 1. So the answer is B- 1.

Prashant said:   8 years ago
@Vishwas.

Thank you, your explanation was easy to understand.


Post your comments here:

Your comments will be displayed after verification.