Why do we have multiple declarations for main in C?
C does not support function overloading. How can we then have 3 prototypes for main? What is the historical reason for havin开发者_运维技巧g 3 prototypes?
There are only two prototypes for main
that a standard-conforming C implementation is required to recognize: int main(void)
and int main(int, char *[])
. This is not overloading, since there can still only be one main
per program; having a void foo(int, double)
in one program and a char *foo(FILE *)
in another isn't overloading either.
The reason for the two prototypes is convenience: some applications want command-line arguments, while others don't bother with them.
All other prototypes, such as void main(void)
and int main(int, char *[], char *[])
, are compiler/platform-dependent extensions.
精彩评论