4

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

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Yusuf
  • 43
  • 1
  • 4
  • 1
    This question is *not* a duplicate of the suggested target. This is asking about the nature of the `main` function, *not* how to override it with linker commands. – Adrian Mole Aug 18 '22 at 10:01

2 Answers2

6

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

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • One has to make a difference between _conformance_ to the C standard and _strictly conforming_ programs. A program using an implementation-defined form of main() may be conforming, but it is not strictly conforming. This is where Bjarne Stroustrup and others are confused when they say that `int main (void)` is the only allowed form of main(). Which is only true when speaking of strictly conforming programs. Normal conforming C programs contain implementation-defined behavior, however. Changing the name and format of main() is common and supported by the C standard. – Lundin Aug 18 '22 at 10:16
  • So if a program does not contain a "main" function a linker option has been used when it was compiled, to give a different name to the entry-point procedure? – Yusuf Aug 18 '22 at 10:46
  • Regarding the quotes, 5.1.2.2.1 is the _hosted_ sub chapter so it's only half of the truth, the other half is found in 5.1.2.1. And Annex J is an informative summary - the relevant normative parts to quote are the ones referred to _from_ the Annex J, in your case 5.1.2.2.1 §3: "or in some other implementation-defined manner". – Lundin Aug 18 '22 at 10:48
  • @Yusuf Not necessarily. Some freestanding systems may default to `void reset (void)` or such, which is the reset interrupt vector. Windows compilers may default to WinMain, and so on. What's important is that the compiler must document all such implementation-defined behavior. – Lundin Aug 18 '22 at 10:50
  • @Lundin IIRC, the `WinMain` function is called by the RTL, from a function called `main` that is defined inside that library. – Adrian Mole Aug 18 '22 at 10:51
4

C allows two forms of systems: freestanding and hosted. A freestanding system is a program without an OS, a program running on a RTOS or the OS itself. A hosted system is something where programs are running on top of an OS such as Linux or Windows.

In freestanding systems, the name and form of the function called when the program starts is compiler-specific. They may use int main (void) or something else entirely. Most common is void main (void).

In hosted systems, two standardized forms exist: int main (void) and int main (int argc, char* argv[]). The standard does however, subjectively, allow implementation-defined forms as well.

What's important to realize is that the form of main() or equivalent is always decided by the compiler and never by the programmer. Programmers cannot use forms that are not documented as valid by the compiler.

Furthermore, a program which is strictly conforming = no compiler-specific behavior, may only use either
int main (void) or
int main (int argc, char* argv[]).

int main (void) is special since it is the only function that may be written without an explicit return and still return 0. Also, returning from main() is equivalent to calling the exit() function.

Details here: https://stackoverflow.com/a/31263079/584518

Lundin
  • 195,001
  • 40
  • 254
  • 396