One can think of the main() function as any other overwritten function that allows either no parameters, i.e. void:
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){}
int main(void) { /* ... */ }
or two parameters:
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){}
int main(int argc, char *argv[]) { /* ... */ }
So when you call it, you need to use one of the two existent definitions.
or equivalent; or in some other implementation-defined manner.
Regarding the parameters:
Regarding the parameters:
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:
So, the two possibilities are handled as:
- If no parameters are declared: no parameters are expected as input.
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.
- 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.
In memory:
they will be placed on the stack just above the return address and the saved base pointer (just as any other stack frame).
At machine level:
they will be passed in registers, depending on the implementation.