would somebody please, tell me a little about what these are... i have done the tutorial for it on the site, but i don't really know what its talking about... what are the arguments for and where does the input come from
Printable View
would somebody please, tell me a little about what these are... i have done the tutorial for it on the site, but i don't really know what its talking about... what are the arguments for and where does the input come from
if you run your program from the commandline like this
then argc will be 4Code:myprog par 1 no
argv[0] will be "myprog"
argv[1] will be "par"
argv[2] will be "1"
argv[3] will be "no"
Kurt
i still don't understand, what does that mean?
> what are the arguments for and where does the input come from
Have you ever used the command line before now?
Like typing
dir
at the console prompt?
Well it's just like that, so if you did
dir /b file.txt
Then you would get to "/b" and "file.txt" via the argc/argv parameters passed to main()
You surely used functions with some arguments before, didn't you? so, let's say you have something like this:
Here you've got 2 arguments, number of type int and ch of type char. You use it within your program, to interact with functions.Code:void myFunc(int number, char ch);
Now let's see this:
That's your program, right? You can also give your program some arguments, like any other function but you have to do that from the command line (in DOS-Box).Code:int main(int argc, char *argv[])
{
...
return 0;
}
Your first parameter (argument) is of type int and it tells you how many arguments the user passed to the program (Attention, program name is included in this count). So when you type
argc = 2;Code:myprog.exe arg1
And ZuK has already written how to use argv. In these example the first argument is agrv[0] = "myprog.exe", the second argv[1] = "arg1" and so on ...