4

As far as I know int main(void) is demanded by C99 an C11 standards. So int main() is not correct for the main function in this standards.

But which C (not C++) standard allows a int main() definition of the main function?

Thanks and regards Alex

Mohan
  • 1,871
  • 21
  • 34
knowledge
  • 941
  • 1
  • 11
  • 26
  • 6
    C and C++ are different languages, the syntax is similar, but the semantics is not. There's no C standard which allows for a `main` function with unspecified arguments, and in C `int somefunction()` means something different from `int somefunction(void)`. – Some programmer dude Oct 08 '15 at 09:29
  • 1
    @JoachimPileborg, and it means even different things if this is found in a declaration (only) or in a definition. – Jens Gustedt Oct 08 '15 at 09:40
  • So why my C compiler also except int main() { } as well as int main(void)? – knowledge Oct 08 '15 at 10:22
  • 2
    Possible duplicate of [Is int main() { } (without "void") valid and portable in ISO C?](http://stackoverflow.com/questions/29190986/is-int-main-without-void-valid-and-portable-in-iso-c) – edmz Oct 08 '15 at 11:14
  • Possible duplicate of [Difference between int main() and int main(void)?](http://stackoverflow.com/questions/12225171/difference-between-int-main-and-int-mainvoid) – Colonel Thirty Two Nov 02 '15 at 14:13

3 Answers3

4

I personally clearly prefer the version with (void), because it is usually better to declare functions with a prototype. But the form int main() { ... } is correct, too, as long as you use it in a definition and not a declaration, and in fact the C standard uses this form in a number of examples.

Here this defines and declares a function with no prototype, but for a definition it is clear that that function doesn't receive any arguments.

If you are trying to give a forward declaration of main, you shouldn't use that form, because there would be no warning if you called the function incorrectly. Here C and C++ are also different since C allows you to call main yourself, even recursively, where C++ forbids such things.

psmears
  • 26,070
  • 4
  • 40
  • 48
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
1

According to C89 (http://web.archive.org/web/20030222051144/http://home.earthlink.net/~bobbitts/c89.txt) the main method is defined in two ways

int main(void)
{
    // ...
}

or

int main(int argc, char *argv[])
{
    // ...
}

As far as I know this is the first standard definition, so I would assume int main() is only defined well in the C++ standard.

However, I still use it :)

Pedro Isaaco
  • 404
  • 3
  • 10
  • The linked C89 standard also says: "Program startup... It can be defined with no parameters..int main(void) { /*...*/ }" I can't see anything about a int main() – knowledge Oct 08 '15 at 10:10
0

If you are still unsure what to use after the other answers, i would recommend to have a define for empty parameters:

#if 0
 #define NO_PARAM
#else
 #define NO_PARAM void
#endif

in case of the main function you could then write

int main(NO_PARAM){
...
}

If you then change your mind later, you can simply use the preferred define without having the need to change all function signatures - which shall have no parameters - by hand.

Magnus Lutz
  • 558
  • 3
  • 12