2

Recently I came across this micro article where the following is stated:

  1. In your C program, you can have only one "main" function, whether it is called "main" or otherwise. If you use IPA, IPA will terminate with an error message issued when more than one "main" function is detected.

Do I understand correctly that the main name (or some other name which is explicitly defined as a replacement of int main() for an entry point) is an important part and, for example, I can have int main(int argc, char **argv) and int sub_main(int argc, char **argv) at the same program?

If not, and if there is a main as part of the functions' names and/or (int argc, char **argv) as parameters I might have a problem, will changing the places of the parameters to int sub_main(char **argv, int argc) make any difference?

I haven't had problems so far so assume int main(int argc, char **argv) and int sub_main(int argc, char **argv) can happily coexist. Still, might be handy to know for sure.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Basically: use the form that your compiler documentation tells you to use. The form of main() is decided by the _compiler_, never by the programmer. – Lundin Nov 26 '21 at 11:22

2 Answers2

4

According to the C standard, you must have one and only one main function in your code, and this is where execution starts from. C defines the following declarations only :

  • int main(void)
  • int main(int, char **) (or equivalent, say char *argv[])

There may also be (as with any C function, only one definition of this, among all files (but there may be any number of declarations).

sub_main() or any other variant that is not exactly main() is perfectly legal. A function's name and its type are distinct - names are unique, types needn't be.

So for void fn(void);, fn is the "name" (also called "identifier"), while void (void) is the type (function that takes no parameters and returns nothing).

When you wish to know for sure, be sure to ask others , like you did here, but also be sure to correlate people's answers with the C standard where it matters.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user426
  • 213
  • 2
  • 9
  • Not exactly. The main function is required to return an int value and to have either no parameters (your `int main(void)` that can also be written `int main()`, `void` is not required per standard here) or to take **at least** 2 parameters, an `int` and a `char **` (your `int main(int, char**)`. But it is allowed to have additional parameters. For example POSIX systems allow to directly pass the environment as `int main(int argc, char ** argv, char **environ)`. – Serge Ballesta Nov 26 '21 at 11:21
  • @SergeBallesta what C standard do you refer to ? `void` is required to indicate "no arguments" in C, plain `()` implies an arbitrary amount of unknown arguments which is highly unhelpful, and not worth mentioning to a new programmer (as OP is) at all. As for additional parameters, as you say, they are extensions to the C standard and I'm not talking of that here. – user426 Nov 26 '21 at 11:22
  • Implementation-defined forms of main are allowed. So it matters if you are merely speaking of C standard compliance or a strictly conforming C program (formal term) with no implementation-defined aspects. – Lundin Nov 26 '21 at 11:24
  • 1
    @SergeBallesta `int main()` might *work* (on some [most?] implementations...), but IMO it's not valid per [C11 **5.1.2.2.1 Program startup**, paragraph 1](https://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1), which lists only `int main(void)`, `int main(int argc, char *argv[])` and "some other implementation-defined manner". I see no reason why `int main(void)` and `int main()` must have the same calling convention in any implementation, I'd say `int main()` is *de facto* a varargs function while `int main(void)` explicitly has no arguments. They don't have to be equivalent. – Andrew Henle Nov 26 '21 at 11:56
  • 1
    @user426: `()` indicates an arbitrary number of unknown parameters only in a function declaration that is not a definition. In a function definition, it is necessarily accompanied by an empty *declaration-list* (from the grammar in C 2018 6.9.1 1), and the function is defined to have no parameters. – Eric Postpischil Nov 26 '21 at 12:08
  • 1
    @AndrewHenle: C 2018 5.1.2.2.1 1 does not say `main` must be declared only as `int main(void)` or `int main(int argc, char *argv[])` or some other implementation-defined manner. It also allows equivalent definitions. In a function definition, `int main() { … }` defines the function to use no parameters, because its *declaration-list* (after the`()` and before the `{`) is empty, making it equivalent to `int main(void) { … }`. (Note this equivalence was needed so that old source code would continue to be valid under the new C standard.) – Eric Postpischil Nov 26 '21 at 12:13
  • @AndrewHenle As usual when this topic pops up... 5.1.2.2.1 is about _hosted implementations_ only. Freestanding implementations use custom forms, see 5.1.2.1. `void main (void)` is by far the most common form in freestanding systems, though some systems don't use main() but launch directly from the reset vector of a microcontroller. As for 5.1.2.2.1 it allows "equivalent forms", see foot note 10) below the part you quote. – Lundin Nov 26 '21 at 12:29
  • All of this is explained in detail, with quotes and sources below [What should main() return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c), in particular see the answers by Jonathan Leffler and yours faithfully. I used it as a dupe target since it answers the question but someone re-opened the question... – Lundin Nov 26 '21 at 12:31
  • @EricPostpischil *..making it equivalent to `int main(void) { … }`* Why must a function with an empty *declaration-list* be equivalent to explicitly having zero arguments because the function is defined as `...(void)`? *Note this equivalence was needed so that old source code would continue to be valid under the new C standard.* That is an implicit requirement that `int main()` and `int main(void)` are equivalent in all ways. I don't see how C11 6.7.6.3p15 requires `int main()` and `int main(void)` to be compatible. – Andrew Henle Nov 26 '21 at 12:32
  • @Lundin There seems to be a hole in the logic, then. C11 5.1.2.2.1p1 says "equivalent" forms of `main()` can be used, but I can't see how C11 6.7.6.3p15 requires `int main()` and `int main(void)` to be compatible functions. And if they're not required to be compatible, I don't see how they must be "equivalent". An implementation could use different calling conventions, for example, even though probably almost every implementation doesn't. – Andrew Henle Nov 26 '21 at 12:37
  • [C11 6.7.6.3p15](https://port70.net/~nsz/c/c11/n1570.html#6.7.6.3p15) "For two function types to be compatible, both shall specify compatible return types. Moreover, the parameter type lists, if both are present, shall agree ..." In this case, one has a parameter type list and one doesn't. If one is missing a parameter type list and one isn't, what does the standard *require*? I don't read it as requiring compatibility. – Andrew Henle Nov 26 '21 at 12:40
  • 2
    @AndrewHenle: C 2018 6.7.6.3 15 says “… If one type has a parameter type list [which `int main(void)` does] and the other type is specified by a function definition [which `int main() { … }` is] that contains a (possibly empty) identifier list [which `int main()` does], both shall agree in the number of parameters [both zero], and the type of each prototype parameter shall be compatible with the type that results from the application of the default argument promotions to the type of the corresponding identifier [trivially satisfied since there are no parameters].“ So the types are compatible. – Eric Postpischil Nov 26 '21 at 12:43
  • @AndrewHenle It is there: "For two function types to be compatible, both shall specify compatible return types." /--/ "If one type has a parameter type list and the other type is specified by a function definition that contains a (possibly empty) identifier list, both shall agree in the number of parameters, and the type of each prototype parameter shall be compatible with the type that results from the application of the default argument promotions to the type of the corresponding identifier." – Lundin Nov 26 '21 at 12:46
  • 1
    ...and Eric was FGITW I noticed just now. – Lundin Nov 26 '21 at 12:47
  • @Lundin "*5.1.2.2.1 is about hosted implementations only.*" This is important , as it is a meaningful point that I left out in my answer for the sake of OP's clarity and simplicity. Thanks for bringing it up in the comments. – user426 Nov 26 '21 at 14:38
0

Only the exact name of the function "main" is important fot the compiler to know where the program starts.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65