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

Vishwas said:   1 decade ago
Let us understand the declarations first:
1)int fun(int, int);
==> fun is a function which takes two integers as input arguements and returns an integer value.
2)typedef int (*pf) (int, int);
==>a)pf is a pointer to a function with two integer arguements and whose return value is also an integer.
==>b)Also we have used the keyword typedef, meaning we want to define our own type "pf" whose characteristics are as described above in 2a)
3)int proc(pf, int, int);
==>proc is a function with 2 integer arguements and pf- our user defined type as explained in 2)

Program flow:
4)proc(fun, 6, 6) called from main,
now we have passed the address of the function fun(which will be copied into function pointer pf), also 2 arguements 6,6 to proc
5)return ((*p)(a, b))
which will inturn call function fun as (*p)(a,b)==>fun(6,6)
6)now 6==6 will be checked in fun which will yield 1 that will be returned to the calling function proc
7)now proc will return(1),1 is returned back to main and hence 1 will be printed.
(5)

Abhishek said:   1 decade ago
@Ashish.

#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?????????


In c there is no strict checking in compilation time it means that u can pass 1 value during the function calling time or at receiving time function parameter once time a program has been compiled by compiled so it would run....

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

proc(fun, 6,6)// it would be changed as proc(address of fun, 6,6)

Address of fun containing the value of a=9

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));
}

MANUKUNDLOO said:   1 decade ago
It is one of the delicious code ever I seen.

As we see here 3 types of function declration used.

Step1: When the compiler read the main function, the cursor will go at proc function.

Step2: In this function no one look the concept of pointer, the value of a and b just access in to (*p) pointer, which operates as a=6,b=6,using typedef int (*pf) function.

Step 3: After that fun(int,int) executin after taking the values, here a==b, hence it returns true value as 1, which read by proc function and gives the value "1", after the execution of main function.

When we change the value of b=7, it gives the value"0".
(1)

Sumit Tripathi said:   6 years ago
In main the first line

printf("%d\n",proc(fun,6,6));
is calling proc which is taking argument a function pointer and two integer values. Function pointer pf is defined as typedef int(*pf) (int,int); This line printf("%d\n",proc(fun,6,6)); will call the function defined as:

int proc(pf p,int a,int b){
return ((*p)(a,b));
}
Now in this function pf holds the pointer to function fun. This will cause the function fun to be called which is returning whether the values of a and b are true or not. Since you have passed 6,6 as the arguments the result will be true and that is why you are getting as 1 as an Answer.
(3)

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

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.

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

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

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


Post your comments here:

Your comments will be displayed after verification.