Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the main function have to be called main?

Tags:

c

function

In C, can the main function be called something else? Is there something special about the name, "main," or is it arbitrary?

like image 816
Yusuf Avatar asked Oct 22 '25 10:10

Yusuf


1 Answers

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 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[]) { /* ... */ }

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 the main function may be defined

like image 132
Adrian Mole Avatar answered Oct 23 '25 23:10

Adrian Mole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!