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.

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)

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)

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.

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.

Prasanthi said:   1 decade ago
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.

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!

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.

Siri said:   1 decade ago
Simple:
The return statement in proc function is replaced like this:
return( fun(a,b));
Which means calling the function fun(a,b).
Since it returns a==b and integer value of true is 1, the output is 1.

Ravitheja said:   1 decade ago
if (a=b), i.e if both values are equal, then it returns 0;
if (a<b), i.e if a is less than b, then it returns -1;
if (a>b), i.e if a is greater than b, then it returns 1;

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)


Post your comments here:

Your comments will be displayed after verification.