What do parameters in main() do?

Jun 3, 2012 at 5:15pm
E.g. int main( int argc, char** argv ) ?

I am running into code like this:

1
2
3
4
5
6
7
8
9
10
11
int main( int argc, char** argv )
  {
  // If necessary, give the user instructions
  if (argc < 3)
    {
    cout <<
      "Convert a UTF-8 file to a wchar file.\n"
      "usage:\n  " << argv[ 0 ] << " UTF8-FILENAME WCHAR-FILENAME\n";

    return 1;
    }


Which looks fantastic and compiles (in context) but makes no sense to me and I think the parameters in main might have something to do with it? I am hoping to learn something that wil allow me to read more type sof code and to learn how it works. Thanks!
Jun 3, 2012 at 5:26pm
argc and argv are details of the input parameters.

Let's imagine your program is run by typing this:

someProgram firstParameter secondParameter


Then I would expect argc to be 3, argv[0] to be (a pointer to the char array containing) someProgram, argv[1] to be (a pointer to the char array containing) firstParameter and argv[2] to be (a pointer to the char array containing) secondParameter.
Jun 4, 2012 at 7:00pm
You will probably hear them referred to as "command-line arguments", which simply means additional arguments given when starting the program through the command line.
Jun 5, 2012 at 12:57pm
closed account (zb0S216C)
Adding to Moschops' answer:

argc is an abbreviation for argument count, and argv is an abbreviation for argument vector. argc specifies the number of arguments in argv.

Wazzak
Topic archived. No new replies allowed.