In C, can the main function be called something else? Is there something special about the name, "main," or is it arbitrary?
Is there something special about the name main or is it arbitrary?
The name, "main," for the entry point of a C program (in a hosted environment) is not arbitrary but is defined by the C Standard:
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 asargcandargv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
In C can the main function be called something else?
Most mainstream toolchains used to compile/build C programs do offer ways to use other names for the entry-point procedure: typically, a linker option explicitly specifying the name, as outlined here. However, such programs do not strictly conform to the C Standard and may cause portability issues, as indicated in "Annexe J" of the cited draft C Standard:
J.3 Implementation-defined behavior
…
J.3.2 Environment
…
— The name and type of the function called at program startup in a freestanding environment
— An alternative manner in which themainfunction may be defined
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With