Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have multiple declarations for main in C?

Tags:

c

C does not support function overloading. How can we then have 3 prototypes for main? What is the historical reason for having 3 prototypes?

like image 500
Bruce Avatar asked Dec 28 '25 02:12

Bruce


1 Answers

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.

like image 111
Fred Foo Avatar answered Dec 30 '25 15:12

Fred Foo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!