C Programming - Functions - Discussion

1. 

Which of the following statements are correct about the program?

#include<stdio.h>

int main()
{
    printf("%p\n", main());
    return 0;
}

[A]. It prints garbage values infinitely
[B]. Runs infinitely without printing anything
[C]. Error: main() cannot be called inside printf()
[D]. No Error and print nothing

Answer: Option B

Explanation:

In printf("%p\n", main()); it calls the main() function and then it repeats infinetly, untill stack overflow.


Ankur said: (Nov 29, 2010)  
it generate error becouse main function can not call inside the main() function.

Lav said: (Nov 17, 2011)  
What is %p ?

Rupinderjit said: (Dec 2, 2011)  
%p is used to print the address of some variable as used by the host computer.It can be Hex Binary BCD unsigned or any.

Varun said: (Aug 22, 2012)  
Thank you. @Rupinderjit.

Dhiraj said: (Nov 30, 2012)  
Why it does not print 0 infinite times ? When stack overflows main return 0 and that value get printed.

Krishanu said: (May 26, 2015)  
Again I want to say that the actual output will be:

"Segmentation fault (core dumped)".

Lakshmi said: (Jul 29, 2015)  
What is the meaning of "Segmentation fault"?

Cheeku said: (Aug 22, 2016)  
It will print the address of main.

Prashanth said: (Aug 31, 2016)  
Output : Segmentation fault (core dumped).

Asish Panigrahi said: (Sep 11, 2016)  
%p has no value so that it prints infinite garbage value.

Yamini said: (Aug 15, 2017)  
I didn't understand. Please give us a clear explanation.

Gourav said: (Aug 27, 2017)  
In this question, main() is called recursively & for each call of main() the memory is allocated in stack & when the stack gets full the pointer which allocates memory will reach in another segment & it will give Segmentation fault (core dumped).

Dawood Ibrahim Bhat said: (Feb 13, 2019)  
Will run infinitely, but STACK-OVERFLOW will occur as the main () is being called continuously and control is not going back to the calling function.

Jayanti said: (Jul 6, 2019)  
Here, Segmentation fault error.

Saha said: (Aug 4, 2021)  
%p is used to printing the address of a pointer.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.