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

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

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

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

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

Ashish said:   1 decade ago
#include<stdio.h>
int fun(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)
{
return (a=9);
}
int proc(pf p, int a, int b)
{
return ((*p)(a, b));
}

guys please help me out ...this program is returning 9 as output how it is possible? bcoz pf is defined as taking 2 variables of type int and returning an int..but in this program i mentioned only a function fun with single argument.so as i think program must show error but it is providing a output 9.can someone tell me how it is possible?????????

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

Pratibha said:   1 decade ago
@Ashish

Your fun() code is this here

int fun(int a)
{
return (a=9); //a is assigned to 9 not compared.
}

:)

Amar said:   1 decade ago
Can anyone please explain me the flow of control of dis program?? :)

Vicky said:   1 decade ago
Return statement only returns one value.

Susanta said:   1 decade ago
What is the type of pf ? please help.


Post your comments here:

Your comments will be displayed after verification.