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)

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)

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)

Raj said:   3 years ago
Thanks @Vishwas.
(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)

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)

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)

Prasadreddy said:   1 decade ago
What is pf p. Let any one explain me?

Veer said:   1 decade ago
Simply. Return statement only returns one value i.e. rightmost value.

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.


Post your comments here:

Your comments will be displayed after verification.