11

Possible Duplicate:
Standard way to define parameter-less function main() in C

Can I use a declaration definition of function main() in C that looks like:

int main() {}

Yes, I saw that standard says that there are only two guaranteed-supported versions:

int main(void) {}

and

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

But what about empty paratheses? I know that it has another meaning than in C++ (in C, it means that number and types of parameters of this function isn't known), but I saw really much code in C with this declaration definition of main.

So who's wrong?

Community
  • 1
  • 1
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • 3
    The short answer is "you can probably get away with it, because your compiler is probably on a pretty lenient setting, but at the same time you probably shouldn't try, because it's mildly wrong, damages portability slightly, may invoke compiler warnings, and brings you no benefit at all." –  Aug 27 '12 at 17:43

4 Answers4

10

In C, there's a difference between the declarations int main(); and int main(void); (the former declares a function with an unspecified number of arguments, and the latter is actually called a proto­type). However, in the function definition, both main() and main(void) define a function that takes no arguments.

The other signature, main(int, char**), is an alternative form. Conforming implementations must accept either form, but may also accept other implementation-defined signatures for main(). Any given program may of course only contain one single function called main.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Want to mention Section J.5 Common Extensions in the C99 standard? _§J.5.1 Environment arguments / ¶1 In a hosted environment, the main function receives a third argument, `char *envp[]`, that points to a null-terminated array of pointers to char, each of which points to a string that provides information about the environment for this execution of the program (5.1.2.2.1)._ – Jonathan Leffler Aug 27 '12 at 17:36
  • 1
    it is not true your first sentence: `main()` takes an unknown number of arguments; only `main(void)` takes no arguments – Federico Aug 27 '12 at 17:40
  • 2
    It might be worth noting that while `int main() {...}` as definition does indeed define a function with no arguments, it nevertheless does not by itself introduce a prototype for `main`. I.e. after such definition `main` is still *declared* as a function with unspecified parameters. The "no arguments" bit affects to the body of `main` only. – AnT stands with Russia Aug 27 '12 at 17:40
  • You mean that in C there are difference about where empty paranthesses are placed - in function declaration or function definition? function declaration with empty () - function with an unspecified number and types of arguments, function definition with empty () - function takes no arguments. Right? Can you please post quote from the Standard if you can? – FrozenHeart Aug 27 '12 at 17:57
  • @NikitaTrophimov: The linked "possible duplicate" topic provides standard references. – Kerrek SB Aug 27 '12 at 18:05
3

int main() and any other function declaration like this, it takes an unknown number of arguments, so this is absolutely wrong for the main function. int main(void) it takes no arguments.

char* argv[] is the argument vector. When you write your argument on the command line you will find arguments in this vector of strings. Sometimes you can also find char **argv but it is the same. The parentheses [] are empty because we don't know how many arguments come from the user; the int argc argument count exists for this purpose: it counts how many arguments are in argv (though the list is terminated with argv[argc] == NULL as a sentinel value too).

Read also this link for the difference between a generic foo() and foo(void)

Community
  • 1
  • 1
Federico
  • 3,782
  • 32
  • 46
  • 1
    As a declaration, `int main()` is problematic; in a function defintion (where the body is also provided), it says that the function takes no arguments, but does not provide a prototype declaration of the function, so if you recursively call `main()`, you won't be prevented from making incorrect calls. This is not typically a major issue! – Jonathan Leffler Aug 27 '12 at 17:38
  • _This is absolutely wrong for the main function_ if you call main explicitly somewhere in your program, which is probably more to avoid than `int main(){}` – AProgrammer Aug 27 '12 at 18:50
2

If the implementation explicitly documents int main() (with no arguments) as a valid signature, then as of C99 everything's fine (§5.1.2.2.1 ¶1, "... or in some other implementation-defined manner.").

If the implementation doesn't document it, then strictly speaking the behavior is undefined (§4 ¶2), but the odds of it leading to behavior significantly different from int main(void) are, in my experience, pretty darned low.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1
   int main() {}
   this is the standard prior to the c99 standard of main method.

   int main(void){}
   this is the standard coined by ANSI.

   int main(int argc, char* argv[]) {}     
   This is the another version of main which provides the user to pass the command line
   argument to the main method.
kTiwari
  • 1,488
  • 1
  • 14
  • 21