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.

Robert said:   9 years ago
Both fun and main are returning integers data types.

int(*p)() = a pointer to a function
Since we can treat a function name like a pointer holding its address so we can access it :

int(*p)() = main (where remember int(*p)() is a pointer to a function and main is also a pointer to the address of function main)

The "main" the argument of the fun is not used in any statement here, thereby it goes with the execution of fun() => the body of the fun will be executed untouched => prints Hello => gives back control to the main function via return 0.
In the main function, after the body of fun() got executed and it returns => prints Hi and exits => answer Hello Hi.

Govind said:   1 decade ago
Hi all,

For those who are thinking what is the use of passing pointer to the main function.

Well, please don't confuse your self.

The function 'int fun (int (*p)())' is a function which takes a function pointer as an argument and returns an integer. And '*p' is a pointer to such a function which takes no argument but return int. Just like our main function.

Now, when the programme starts, main will call fun passing the address of main function. But in the function 'fun' we are not using the address of the main at all. We are just passing it.

So in fun it will only print 'Hello' and return to the calling function where it prints 'Hi' and terminates.

I hope this helps.

Thanks.
(1)

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.

Nagu said:   8 years ago
Why the infinite loop not exist when main was used?

The infinite loop does not exist because of the sub-function not recursive function so not infinite loop exists.

The sub-function print "hello" then come to the main function. Then print "hi" end the program.

So not confusing the pointer argument that only pass main program address that not used in sub-function.

Thanks.

Tinky said:   1 decade ago
Adding to @Raju comment:
If I pass any arguments on the calling functions then how will the function work and what is the use of it?

Passing a variable (by value or reference) has practical implementations in Projects, but what is the use of passing a function? Can we access the variable of function if we pass them as argument?

Thanks in advance,

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.

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

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.

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

SSFD said:   1 decade ago
The address of main was passed just to complicate things and test whether you can understand the concept or not. In actual program none is going to do so.


Post your comments here:

Your comments will be displayed after verification.