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));
}
Discussion:
49 comments Page 4 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.
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)
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;
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;
Nayana said:
1 decade ago
@Vasuroshan, thank you for your explaination.
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".
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)
Gopichand said:
1 decade ago
Please any one can explain me clearly.
Susanta said:
1 decade ago
What is the type of pf ? please help.
Rita said:
1 decade ago
Thanks aashish.
Amar said:
1 decade ago
Can anyone please explain me the flow of control of dis program?? :)
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.
}
:)
Your fun() code is this here
int fun(int a)
{
return (a=9); //a is assigned to 9 not compared.
}
:)
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?????????
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?????????
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers