Just wondering why this
int main(void){}
compiles and links
and so does this:
int main(int argc, char **argv){}
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 of
intand with no parameters:
int main(void) { /* ... */ }or with two parameters (referred to here as
argcandargv, 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.
The first counts the arguments supplied to the program and the second is an array of pointers to the strings which are those arguments. These arguments are passed to the program by the command line interpreter. So, the two possibilities are handled as:
If no parameters are declared: no parameters are expected as input.
If there are parameters in
main(),they should:
argcis greater than zero.argv[argc]is a null pointer.argv[0]through toargv[argc-1]are pointers to strings whose meaning will be determined by the program.argv[0]will be a string containing the program's name or a null string if that is not available. Remaining elements ofargvrepresent the arguments supplied to the program. In cases where there is only support for single-case characters, the contents of these strings will be supplied to the program in lower-case.
they will be placed on the stack just above the return address and the saved base pointer (just as any other stack frame).
they will be passed in registers, depending on the implementation.