We have seen to declarations for main():
void main(void); // no parameters, no return
int main(void); // no parameters, returns an int
main() can also be declared to accept 2 or 3 parameters:
void main(int argc, char *argv[]);
- argc is the number of arguments (parameters) that were typed on the command-line; naming the first parameter argc is a convention
- argv is an array of strings representing each command-line argument; naming the second parameter argv is a convention; don't worry about the *, Just Do It!
When you run a program, you type the name of the program followed by 0 or more parameters:
program param1 param2 param3 param4 . . .
These parameters are passed along to the program as an array of NULL terminated strings. We have been passing parameters to programs since the first day:
command-line argc
pico program6.cpp 2
vi program6.cpp 2
xlC -o lab6 program6.cpp 4
mail -s "Subject" user@computer.domain 4
ftp tsx-11.mit.edu 2
telnet pccaix.sycrci.pcc.edu 2