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 3 of 5.
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));
}
#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));
}
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.
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.
Prasadreddy said:
1 decade ago
What is pf p. Let any one explain me?
Vicky said:
1 decade ago
Return statement only returns one value.
Veer said:
1 decade ago
Simply. Return statement only returns one value i.e. rightmost value.
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
#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
Siri said:
1 decade ago
Simple:
The return statement in proc function is replaced like this:
return( fun(a,b));
Which means calling the function fun(a,b).
Since it returns a==b and integer value of true is 1, the output is 1.
The return statement in proc function is replaced like this:
return( fun(a,b));
Which means calling the function fun(a,b).
Since it returns a==b and integer value of true is 1, the output is 1.
Ashok said:
1 decade ago
Thank you vishwas.
Diana said:
1 decade ago
Excellent answer!!Vishwas
Nagaraj said:
1 decade ago
Thanks vishwas.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers