How can you pass arguments using
int main (int argc,char *argv[])
How can you pass arguments using
int main (int argc,char *argv[])
If you have a program called "Hello.Exe" for example and you type the following:
Hello C:\Temp C:\Windows 500 -X
Then what gets passed into the main function via the argc and argv variables are:
argc = 5
argv[0] = "Hello" <- Actually this may be something like C:\Progs\Hello.exe
argv[1] = "C:\Temp"
argv[2] = "C:\Windows"
argv[3] = "500" <- If you want this to be an int, you will need to convert it.
argv[4] = "-X"
If you want to create a simple test program to try this out, compiler and run this:
Run it at the command prompt and pass in some arguments, numbers, character strings, etc. See what happens!Code:int main( int argc, char* argv[] ) { int iLoop; for( iLoop = 0; iLoop < argc; iLoop++ ) prntf( "Argument %d is %s.\n", iLoop+1, argv[iLoop] ); return 0; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
>int main (int argc,char *argv[])
argc - number of variables, at least 1, since the progrram name is also counted
argv - the parameters on the command line, argv [0] is the program name
my problem was how to run it. Now i went to the command promt and it works thank you .