spb
spb

Reputation: 175

How can the main function have 2 declarations/definitions in C?

The main() function in C has multiple definitions.

int main (void) { body }    (1) 
int main (int argc, char *argv[]) { body }  (2) 

(https://en.cppreference.com/w/c/language/main_function).

How is this possible since C cannot have multiple declarations/definitions of the same function?

Upvotes: 1

Views: 96

Answers (1)

dbush
dbush

Reputation: 223862

The main function is special in that is called by the runtime, and as such it is allowed to have one of those two signatures but not both at once.

Presumably, the runtime pushes the required arguments onto the stack as part of the call to main which doesn't have to use them. Since this is considered part of the implementation it can do things normal programs can't (or shouldn't) ordinarily do.

Upvotes: 6

Related Questions