int main(int argc, string argv[ ]) {
//do something
}
And these two arguments, argc and argv, enable you to know what data the user has provided at the command-line and how many things they provided at the command-line.
Argc stands for argument count, and you should know, by the way, that you could call argc whatever you want it. You can call argv whatever you wanted.
These are just conventional names that we use for them-- argument count, and as we'll see in a second, argument vector, argv. But you don't have to call them argc and argv if you don't want to, but conventionally, that's what we do.
So anyway, argc, the argument count. It's an integer-type variable and so, as you might expect, if we have two things that we're going to be finding out what these are typed and how much stuff the user typed, argc is going to tell us how much stuff the user typed.
So it gives you a number of command-line arguments user typed when the program was executed.
command argc
./greddy 1
./greddy 1024 cs50 3
So if your program is run dot slash greedy, and inside of your greedy program your main function has the declaration int main int argc, string argv square brackets, then argc in that case is one.
Now notice we don't count how many things the user typed after the program name. The program name itself counts as a command-line argument.
So dot slash greedy, in that case, argc is one. If the user typed slash greedy 1024 CS50 at the command-line, argc in that case would be three.
And we know this because the way that the division between the strings is detected is whether there is a space, or a tab, or something like that between them. So any amount of white space, so-called, between the values typed command-line indicates how many there are. So dot slash greedy space 1024 space CS50, argc, in that case, is three.
Argv is the argument vector. Vector, by the way, is just another word for an array, and this is an array that stores strings. One string per element, which is the strings that the user actually typed at the command-line when the program was executed.
Now, as is the case with any array, if you recall from our discussion of arrays, the first element of argv is always going to be found at argv square bracket zero. That's the first index of the argv array.
So that will-- and in fact, that will always be the name of the program, will always be located at argv square bracket zero.
The last element of argv is always found at argv square brackets argc minus one.