A comment on this answer to a StackOverflow question made me curious. According the C99 standard, section 5.1.2.2.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 ofint
and with no parameters:
int main(void) { /*...*/ }
or with two parameters (referred to here as
argc
andargv
,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; or in some other implementation-defined manner.
This was the case in ANSI C as well, it seems; I'm unaware of any earlier standards besides K&R.
The second signature, with two parameters, has always included argc
, the number of arguments in argv
. Given that argv
is a NULL-terminated list, however, it's trivial to calculate this number.
Historically, is there a reason for including argc
as a parameter of main
? Was it a feature of earlier languages such as B? Was it to save computation (at the cost of potentially using a bit more memory)? Was it just a programmer convenience?
char *envp[]
(or equivalentlychar **envp
). There is no count parameter associated with it, it's just a pointer to the first element of aNULL
-terminated array of pointers.