Thread: help Passing Arguments

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    15

    Question help Passing Arguments

    How can you pass arguments using

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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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:

    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;
    }
    Run it at the command prompt and pass in some arguments, numbers, character strings, etc. See what happens!
    "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

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >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

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    15

    Cool thanks for the help

    my problem was how to run it. Now i went to the command promt and it works thank you .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arguments to the system
    By ech0wave in forum C Programming
    Replies: 6
    Last Post: 05-07-2009, 11:15 AM
  2. Passing arguments
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 01:14 PM
  3. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  4. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  5. passing arguments
    By Baard in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2003, 08:10 AM