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

Ashish said:   1 decade ago
@ karthik : step 3 is wrong and....
a==b will return 1 if a=6 and b=6 so fun(6,6) will return 1
to proc(fun,6,6) and which later return 1 to calling function...which ll print the output 1.
check with a=6 and b=7 the output ll be 0 bcoz return(a==b); statement ll return 0 to proc and which returns 0 to calling function later...........

Vasuroshan said:   1 decade ago
I will explain u guys....
first int (*pf)(int,int) is a typedef that is we use(*pf) instead of function prototype (int,int).
Actually it returns 1 if a==b and returns 0 if a!=b,because it calls proc(fun,6,6)...
here pf takes the address of fun...so in return statement we use *p to call the function.....
in fun(a,b)
return the value 1 because a and b values are equal....thats it....
sorry for my english......

Ramya said:   1 decade ago
Please define the proc function in clear manner.

Bagesh Kumar singh said:   1 decade ago
Any one explain me!

Karthik B said:   1 decade 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 then 0;
return 0;
step4 : Now Zero is 1 char so printf in main takes 0 as 1 char
and print 1 at end of program

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

Preethi said:   2 decades ago
Thanks Sundar.

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
What does proc mean and give me its syntax?


Post your comments here:

Your comments will be displayed after verification.