C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 18)
18.
What will be the output of the program?
#include<stdio.h>
int fun(int(*)());

int main()
{
    fun(main);
    printf("Hi\n");
    return 0;
}
int fun(int (*p)())
{
    printf("Hello ");
    return 0;
}
Infinite loop
Hi
Hello Hi
Error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
33 comments Page 1 of 4.

Bagesh Kumar bagi said:   1 decade ago
First fun(main) come in the function and print the HELLO after that it will be return and it print the Hi

So that the finaly output is

hello hi..

Jitu said:   1 decade ago
How? please explain with logic.

Vinod said:   1 decade ago
I think it is a function pointer.

Kunal said:   1 decade ago
Yes it is a function pointer....fun(main) sends a copy of main() function to p and it becomes the argument of fun().it prints hello then return the control to the calling fuction ...then rest of the part is executed.

Manu said:   1 decade ago
But what is the use of passing the copy of main() to the function?

ASHISh said:   1 decade ago
Yes, I agree with Manu.

What is the use of function pointer in this program?

Praveen said:   1 decade ago
Its a pointer function where p is the reference to call the main().

But here function is not referred by p because p is not initialized to the main().

So only its just act as ordinary function parameter and prints 'Hello Hi'.

Ria said:   1 decade ago
Please explain more.

Riyaz said:   1 decade ago
Can anyone provide explanation to this?

Sujith said:   1 decade ago
They did not use the p anywhere in the fun just they printing the statement!


Post your comments here:

Your comments will be displayed after verification.