Thread: int main(int argc, char *argv[]) ?

  1. #1
    Shadow12345
    Guest

    int main(int argc, char *argv[]) ?

    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'.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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:
    Code:
    #include <iostream>
    
    int main ( int argc, char *argv[] )
    {
      if ( argc == 3 )
        std::cout<< atoi ( argv[1] ) + atoi ( argv[2] ) <<"\n";
      return 0;
    }
    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.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    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"

  4. #4
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    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.
    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.).

    Example:

    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;
    }
    If I ran the program on the cmd-line as:
    $:> 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

  5. #5
    Shadow12345
    Guest
    #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?

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    That is the new method. The old method is including iostream.h at the top of the code.

  7. #7
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. I need help with variables, please
    By JOCAAN in forum C++ Programming
    Replies: 39
    Last Post: 12-08-2007, 04:16 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM