Walter Roberson wrote:
Robbie Hatley wrote:
"mike-yue" wrote:
Is it possible to call one main function from another main
function?
No. Not in C. There must be exactly ONE main, with one of these two
signatures:
int main (void)
int main (int, char**)
Citation?
ISO/IEC 9899:1999 (E) section 5.1.2.2.1 :
5.1.2.2.1 Program startup
1 The function called at program startup is named main.
The implementation declares no prototype for this function.
It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the function
in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9) or in some other implementation-defined manner.
Now, you can try to violate that by having functions called
"main" in two different modules, but your compiler is likely
to say some thing like this:
cwd = C:\RHE\src\test
$gcc module1.c module2.c -o C:\bin-test\multmain.exe
module1.c:9: error: conflicting types for 'main'
module1.c:6: error: previous declaration of 'main' was here
module1.c:10: error: too many arguments to function 'main'
module2.c: In function 'main':
module2.c:6: warning: return type of 'main' is not 'int'
cwd = C:\RHE\src\test
$
Yes, maybe you can hide one "main" function by making it "static"
and calling it using pointers. That's what I'd call a "loophole",
however. It thwarts the intentions of the framers of the standard,
and is bad programming. It's like getting around "thou shalt not
commit murder" by working to reduce a person's morale until the
person wants to commit suicide, then handing him some cyanide.
So let me slightly ammend my answer:
"No. Technically, you might be able to come up with some
pointer trickery that will allow you to indirectly call
one function named 'main' from another 'main' in the same
program, but it's bad practice, and you shouldn't do it."
--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant