Re: main(int argc, char *argv[])
- From: "Arthur J. O'Dwyer" <ajo@xxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 29 Apr 2005 10:06:06 -0400 (EDT)
On Fri, 29 Apr 2005, Kenneth Brody wrote:
Don't call main(). Instead, have main() do what it needs to do with argc/argv, then call the recursive function, and have everything call that instead of main().
Good advice. I usually write a function called 'process', int process(FILE *in, FILE *out); and have it do all the work, 'main' being a wrapper around it. So if for some reason I ever needed to perform the whole operation recursively, it wouldn't be too hard. Of course, the OP should think carefully about what he's trying to do; recursing on any significant part of the program is usually the sign of a newbie mistake, as with those beginners who write
int get_user_command_from_menu() {
int choice;
printf("Choose something (1-5).\n");
scanf("%d", choice); /* no error-checking! */
if (1 <= choice && choice <= 5)
return choice;
else {
printf("That was an invalid choice.\n");
printf("Please select 1, 2, 3, 4, or 5.\n");
return get_user_command_from_menu();
}
}In fact, I believe I've seen in this group that calling main() is specifically forbidden by the standard. (Or at least it's not standard behavior, being either "implementation defined" or "undefined behavior".)
Wrong. Calling 'main' recursively in C is perfectly fine and okay. You may be thinking of The Language That C Is Not, in which calling 'main' /is/ disallowed.
HTH, -Arthur .
- References:
- main(int argc, char *argv[])
- From: Sokar
- Re: main(int argc, char *argv[])
- From: Kenneth Brody
- main(int argc, char *argv[])
- Prev by Date: Re: main(int argc, char *argv[])
- Next by Date: Re: Caaling assembly routine in c
- Previous by thread: Re: main(int argc, char *argv[])
- Next by thread: Re: main(int argc, char *argv[])
- Index(es):