Thread: argv and argc command line params

  1. #1
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385

    argv and argc command line params

    how do i accept command line parameters onto a program running under windows, i understand argv is the number of arguements but for eg - if i wanted to accept a 2 as a command line param and act on it - how do i do it.
    tia
    Monday - what a way to spend a seventh of your life

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    68
    i thought argc held the number of arguments where argv held the arguments ??
    "with a gun barrel between your teeth, you speak only in vowels."
    - tyler durden

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Welcome back!!!!!

    I'm assuming you mean a Win32 Program that uses WinMain???

    If so, the third param of WinMain is a LPSTR which will give you the command line as a string ..

    Also, you can use the GetCommandLine() API at any point to get this data......

    You need some sort of way to tokenise the commandline - so look at strtok from the Std C Library

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    i thought argc held the number of arguments where argv held the arguments ??
    That is correct. Except that argc will contain number of args + 1, and argv[0] will contain the name of the program.

    The data is in character format, so to extract a number, do:

    int first_arg = atoi(argv[1]);

    But always remember to check the count in argc, if it is 1, then nothing was entered into the command line, and the program should probably exit gracefully

    By the way, the naming convention is arbitrary, you could equally do:

    int main(int count, char* array_of_commands[])
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        while (--argc)
            printf ("%s ", *++argv);
        return 0;
    }
    
    
    /* OR */
    
    int main(int argc, char *argv[])
    {
        int i;
        for (i = 1; i < argc; i++)
            puts(argv[i]);
        return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 10:13 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  5. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM