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..
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..
The simple reason being that the control always first go to main.Thus it is automatically located by the compiler thus giving its prototype is redundant and is of no use.
Also we use prototype when call to a function is made prior to its definition.Thus looking at the function prototype compiler can decide whether call is legitimate or not.But in case of main we are accustomed to provide its definition along with its declaration (which is logically correct also)thus no need of prototype.
Even when we make our c programs organized into multiple files there is no need for prototype of main.
The original purpose of prototypes was to support forward references to functions that could be processed by single pass compilers.
In other words, if you have something like this:
void function_A(){
printf( "%d", function_B( 5 ) );
}
int function_B( int x ){
return x * 2;
}
function_B is called before it is defined. This will create problems for simple compilers. To avoid these problems, putting prototypes at the beginning of the file, ensures that the compiler knows about all the functions in the file ahead of time, so forward references are not a problem to type check.
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.
C language standard, draft n1256:
Emphasis mine.