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 3 of 4.

Narendra said:   1 decade ago
Everyone just explaining therotically. Please explain in deep and practically line by line.

Dinesh said:   1 decade ago
It is function pointer. Here we are initializing the funtion pointer to the address of the main during function call. Then enter the body of the function. Print "hello" then closing braces (}) comes, control back to the calling funtion then prinf"hi" so answer is hellohi.

Kiran said:   1 decade ago
int fun(int(*)());

In this (int(*)()) means:

It is a function pointer which returns ineger datatype.

Here the pointer points main function. Main function return 0.

Sa our sub function argument is zero. Then sub function will execute and print Hello.

After that is goes to next line and print hai.

Then main() will be terminate.

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

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

Ria said:   1 decade ago
Please explain more.

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

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

What is the use of function pointer in this program?

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

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.


Post your comments here:

Your comments will be displayed after verification.