just wondering if calling main() recursively is a good thing to do?
[code]
int main()
{
//something here
main();
}
just wondering if calling main() recursively is a good thing to do?
[code]
int main()
{
//something here
main();
}
You can not call main recursively per the standard.
what about C99?
Originally Posted by paperbox005
You actually can call main recursively. It is rarely done as it it bad practice and who knows what depth of recursion you can call it too. I actually had tested this one time. But it can be done. Implementing it is up to you.
Thantos may be referring to C++ in that the standard specifically says you cannot call main recusrsively. However in C the standard doesn't explicity state this.
Last edited by caroundw5h; 08-19-2004 at 09:26 PM.
Warning: Opinions subject to change without notice
The C Library Reference Guide
Understand the fundamentals
Then have some more fun
calling main recursively isnt bad but have an eye on the stack space you
have (it doesnt matter but a big program may cause stack overflow
Remove all doubt by doing this
It's just a couple of extra linesCode:int mymain ( int argc, char *argv[] ) { // blah return mymain ( argc-1, argv+1 ); } int main ( int argc, char *argv[] ) { return mymain( argc, argv ); }
main() is unique in that it may have a unique calling convention which is incompatible with recursive calls.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
>main() is unique in that it may have a unique calling convention which is incompatible with recursive calls.
Maybe I'm missing what you are saying here? (It wouldn't surprise me.)Code:#include <stdio.h> int mymain ( int argc, char *argv[] ) { if ( !argc ) { return 0; } printf("argc = %d, argv[0] = %s\n", argc, argv[0]); return mymain ( argc-1, argv+1 ); } int main ( int argc, char *argv[] ) { return mymain( argc, argv ); } /* my output H:\>test one two three argc = 4, argv[0] = H:\test.exe argc = 3, argv[0] = one argc = 2, argv[0] = two argc = 1, argv[0] = three */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I think the point was "Why call main recursively when it takes only a few more lines to call a regular function recursively instead?".
Quzah.
Hope is the first step on the road to disappointment.