#include <iostream>
int main()
{
main();
std::cout<<"Hello World! "<<std::endl;
return 0;
}
This is the code, How does it behave? Why?
#include <iostream>
int main()
{
main();
std::cout<<"Hello World! "<<std::endl;
return 0;
}
This is the code, How does it behave? Why?
That's undefined behaviour. You cannot call main() from within a C++ program (section 3.6.1.3 of the standard).
Therefore anything can happen. And there's no point in asking why.
main() function call itself in C++, what will happen?
Anything can happen, since it's undefined behavior. But as the program is currently standing, some infinite recursion (and eventually a stack overflow) seems reasonable.
You should not call main inside main, it is undefined behavior.
§ 5.2.2.9 Function call
Recursive calls are permitted, except to the function named main (3.6.1).
§ 3.6.1
The function main shall not be used within a program. The linkage (3.5) of main is implementation-defined. A program that defines main as deleted or that declares main to be inline, static, or constexpr is illformed. The name main is not otherwise reserved. [ Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. —end example ]
In C++, calling main()
from your is not allowed. So it is an error. Even taking its address is error.