Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare main prototype

Is there any reason why I never see main's prototype declared in C programs, ie:

int main(int argc, char* argv[]);

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

Always seemed inconsistent..

like image 962
user318904 Avatar asked Feb 16 '11 18:02

user318904


People also ask

What is main prototype?

It's the entry point of your program and it's defined by the standard. for example c standard define it this way: 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.

Why do we declare prototypes in C?

The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks the function signatures before calling it.

Why do we declare prototypes?

1) It tells the return type of the data that the function will return. 2) It tells the number of arguments passed to the function. 3) It tells the data types of each of the passed arguments.

Does main need a prototype?

Since all the valid forms of the main function are known to the compiler beforehand, it is unnecessary to create a prototype, because the compiler can type check a forward reference to main() without it.


1 Answers

C language standard, draft n1256:

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[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner.

Emphasis mine.

like image 80
John Bode Avatar answered Sep 21 '22 11:09

John Bode