Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definition of main() in C [duplicate]

Tags:

c

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?

like image 782
FrozenHeart Avatar asked Aug 27 '12 17:08

FrozenHeart


People also ask

Is it duplicate or duplicates?

verb (used without object), du·pli·cat·ed, du·pli·cat·ing. to become duplicate. exactly like or corresponding to something else: duplicate copies of a letter. consisting of or existing in two identical or corresponding parts; double.

What is multiple definition error in C?

If you put a definition of a global variable in a header file, then this definition will go to every . c file that includes this header, and you will get multiple definition error because a varible may be declared multiple times but can be defined only once.


1 Answers

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.

like image 120
Kerrek SB Avatar answered Oct 14 '22 09:10

Kerrek SB