int main(int argc, char *argv[])
What are the intargc and char *argv[] for? I have seen these in some programs but I am not sure what they 'do'.
int main(int argc, char *argv[])
What are the intargc and char *argv[] for? I have seen these in some programs but I am not sure what they 'do'.
The parameters to main are command line options. If you run your program from the command line you can pass initial values to it with the arguments. For example, you can run a program that calculates a sum that takes user input from the command line:
c:\> program 1 2
The code might look something like this:
The result would be the value 3 being printed to the screen in the command prompt. As you may have guessed, argv contains the actual arguments and argc contains the number of arguments.Code:#include <iostream> int main ( int argc, char *argv[] ) { if ( argc == 3 ) std::cout<< atoi ( argv[1] ) + atoi ( argv[2] ) <<"\n"; return 0; }
-Prelude
My best code is written with the delete key.
Something that you may run into that I've noticed: Im not sure if this is only with XP (I doubt it), but windows seems to automatically send the path of the file as the first command argument. I was treating my program to deal with only one arg. but quickly realized it always got two. Printing the other one showed it to be the path to the file. This wasn't documented in the reference book I used nor have I seen it elsewhere (although I'm sure it's mentioned somewhere)
Just soemthing to look out for.
-------------------
"Exception"
You have to remember the first parameter in argv[] is going to be the name of the program by default (no matter what version of windows, DOS, etc.).Originally posted by DeadArchDown
Something that you may run into that I've noticed: Im not sure if this is only with XP (I doubt it), but windows seems to automatically send the path of the file as the first command argument. I was treating my program to deal with only one arg. but quickly realized it always got two. Printing the other one showed it to be the path to the file. This wasn't documented in the reference book I used nor have I seen it elsewhere (although I'm sure it's mentioned somewhere)
Just soemthing to look out for.
Example:
If I ran the program on the cmd-line as:Code:#include <iostream> using std::cout; using std::endl; int main(int argc, char *argv[]) { if( argc == 1 ) cout << "You did not provide any arguments!" << endl << "The name of the program is: " << argv[0] << endl; else if ( argc == 2 ) cout << "You provided one argument!" << endl << "The first argument is: " << argv[1] << endl; else if( argc == 3 ) cout << "You provided two arguments!" << endl << "Argument #1: " argv[1] << endl << "Argument #2: " argv[2] << endl; else cout << "You entered more than 2 arguments" << endl; }
$:> myExampleProgram
The output would be:
You did not provide any arguments!
The name of the program is: myExampleProgram
=========
If I ran the program on the cmd-line as:
$:> myExampleProgram tag1
The output would be:
You provided one argument!
The first argument is tag1
And I think you get the picture after that.. Just mess around with some code. Good luck
#include <iostream>
int main ( int argc, char *argv[] )
{
if ( argc == 3 )
std::cout<< atoi ( argv[1] ) + atoi ( argv[2] ) <<"\n";
return 0;
}
This is your code Prelude. I don't understand why you used std::cout, I thought that was old and no longer used. Also what is atoi?
That is the new method. The old method is including iostream.h at the top of the code.
Shadow, 'atoi' parses a string and returns the content as an integer. Like....
Code:#include <stdio> #include <stdlib> int main() { int myint; char mynumber[10]; printf ("Give me a number "); gets ( mynumber ); myint = atoi (mynumber); printf ("The number was %d ",i); return 0; }