Can you recurse main?
ie
[code]
int main(void)
{
int x = 0;
cout << x++ << endl;
while(x<40)
main();
}
Printable View
Can you recurse main?
ie
[code]
int main(void)
{
int x = 0;
cout << x++ << endl;
while(x<40)
main();
}
Yes, but it is not advised! Some compilers will give you an error/warning.
--edit-- Like salem said though, it will be an infinite loop, not recursion that has a basecase to stop the recursion.
>> If you really must do it
Though I really see no reason why you ever would need to.
I know. I was just wondering
>>Yes, but it is not advised!
No, C++ doesn't allow recursive calls to main...ever. You can try to compile as C, then you can recursively call main, but that's not the same question.
>>Some compilers will give you an error
Yes, that would be C++ compilers.
>> Yes, that would be C++ compilers.
Only completely standard compliant compilers. I have seen C++ compilers allow people to call main recursively with little more than a small warning to the effect of, "You shouldn't do this."
Im not going to argue, im only going to post this once. Put this in your compiler.Quote:
Originally posted by Casey
>>Yes, but it is not advised!
No, C++ doesn't allow recursive calls to main...ever. You can try to compile as C, then you can recursively call main, but that's not the same question.
>>Some compilers will give you an error
Yes, that would be C++ compilers.
And it will work depending on your compiler! infinite loop! Not exactly recursion, but similar. That was my answer! VSC++ will allow this with no errors/warnings. Some others will not.Code://filename blah1.cpp
#include <iostream>
int main()
{
std::cout << "Blah";
main();
return 0;
}