how to write a program that takes arguments from commandline?

Discussion in 'C Programming' started by =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 11, 2006.

  1. Hi,

    I'm learning C-programming. I have a program which I would like to
    modify so it takes arguments from the commandline. Let call the program:
    program.exe.

    Could somebody shortly explain how I get this behaviour:

    C:>program -help or C:>program -h
    printf("\nBla. bla. Here is some help and arguments\n").... etc.

    C:>program -dt=0.1 -tend=10 (etc. additional switches might be added).
    In the program:

    float (or double) dt should become 0.1. The integer variable named
    "tend" should be assigned respectively to 10.

    In case of any problems such as for instance C:>program dt=adg, the
    program should respond with something like "dt: Invalid syntax. Exiting.".

    I suspect that one should change the program such that "int main(??
    something goes in here, right?)" instead of int main(void), but I'm not
    really sure of how to address this problem. If somebody has any sample
    code in C to post, I would be very happy.

    Thanks in advance for any hints...


    Med venlig hilsen / Best regards
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 11, 2006
    #1
    1. Advertising

  2. In article <>,
    =?ISO-8859-1?Q?Martin_J=F8rgensen?= <> wrote:
    >I'm learning C-programming. I have a program which I would like to
    >modify so it takes arguments from the commandline.


    >I suspect that one should change the program such that "int main(??
    >something goes in here, right?)" instead of int main(void), but I'm not
    >really sure of how to address this problem.


    Ummm, I cannot think of any introductory C text which does not
    cover the mechanics of passing in arguments.

    The harder part is in cleanly and efficiently parsing what gets passed in,
    especially if some options modify the meaning of others or some
    options cannot co-exist with others. For the mechanics, see most
    any program and look for argc and argv .
    --
    "law -- it's a commodity"
    -- Andrew Ryan (The Globe and Mail, 2005/11/26)
    Walter Roberson, Feb 11, 2006
    #2
    1. Advertising

  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Alvin Guest

    You're right, when you define main, you do:
    int main(int argc, char *argv[])
    argc is the number of arguments specified, program.exe would count as
    an argument, too.
    argv is the arguments given. It's a constant array, so it starts at [0]
    (that would be program.exe). [1] would be -h or -help, and so on. I've
    seen argv written as char **argv rather than the conventional char
    *argv[], but I guess it's down to you.

    One problem I've run into is this: when testing for an argument, say:
    if(argv[1] == "-help")
    in your case, it doesn't work. If you try:
    ---
    if(argv[1] == "-help")
    printf("\n<help>");
    else
    printf("\nyou typed %s", argv[1]);
    ---
    then no matter what, it drops to the else statment.
    Alvin, Feb 11, 2006
    #3
  4. "Martin Jørgensen" <> wrote in message
    news:...
    > Hi,
    >
    > I'm learning C-programming. I have a program which I would like to
    > modify so it takes arguments from the commandline. Let call the program:
    > program.exe.
    >
    > Could somebody shortly explain how I get this behaviour:
    >
    > C:>program -help or C:>program -h
    > printf("\nBla. bla. Here is some help and arguments\n").... etc.
    >
    > C:>program -dt=0.1 -tend=10 (etc. additional switches might be added).
    > In the program:
    >
    > float (or double) dt should become 0.1. The integer variable named
    > "tend" should be assigned respectively to 10.
    >
    > In case of any problems such as for instance C:>program dt=adg, the
    > program should respond with something like "dt: Invalid syntax. Exiting.".
    >
    > I suspect that one should change the program such that "int main(??
    > something goes in here, right?)" instead of int main(void), but I'm not
    > really sure of how to address this problem. If somebody has any sample
    > code in C to post, I would be very happy.
    >
    > Thanks in advance for any hints...


    Maybe this is relevant:
    http://c-faq.com/misc/argv.html
    stathis gotsis, Feb 11, 2006
    #4
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Joe Wright Guest

    Alvin wrote:
    [ much snippage ]
    >
    > One problem I've run into is this: when testing for an argument, say:
    > if(argv[1] == "-help")
    > in your case, it doesn't work. If you try:
    > ---
    > if(argv[1] == "-help")
    > printf("\n<help>");
    > else
    > printf("\nyou typed %s", argv[1]);
    > ---
    > then no matter what, it drops to the else statment.
    >

    Wrong completely. 'if (argv[1] == "-help")' can never be expected to
    'work' in your wildest dreams. argv[1] is a char* supplied by the
    command processor, "-help" is a char* to an anonymous array of char
    somewhere in memory. They will not ever be equal. Never.

    strcmp() is your friend.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Feb 11, 2006
    #5
  6. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    boa Guest

    Joe Wright wrote:
    > Alvin wrote:
    > [ much snippage ]
    >>
    >> One problem I've run into is this: when testing for an argument, say:
    >> if(argv[1] == "-help")
    >> in your case, it doesn't work. If you try:
    >> ---
    >> if(argv[1] == "-help")
    >> printf("\n<help>");
    >> else
    >> printf("\nyou typed %s", argv[1]);
    >> ---
    >> then no matter what, it drops to the else statment.

    -------^^^^^^^^^^^^^^^^
    Did you miss this part?

    >>

    > Wrong completely. 'if (argv[1] == "-help")' can never be expected to
    > 'work' in your wildest dreams. argv[1] is a char* supplied by the
    > command processor, "-help" is a char* to an anonymous array of char
    > somewhere in memory. They will not ever be equal. Never.
    >
    > strcmp() is your friend.
    >



    boa
    boa, Feb 11, 2006
    #6
  7. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Joe Wright Guest

    boa wrote:
    > Joe Wright wrote:
    >
    >> Alvin wrote:
    >> [ much snippage ]
    >>
    >>>
    >>> One problem I've run into is this: when testing for an argument, say:
    >>> if(argv[1] == "-help")
    >>> in your case, it doesn't work. If you try:
    >>> ---
    >>> if(argv[1] == "-help")
    >>> printf("\n<help>");
    >>> else
    >>> printf("\nyou typed %s", argv[1]);
    >>> ---
    >>> then no matter what, it drops to the else statment.

    >
    > -------^^^^^^^^^^^^^^^^
    > Did you miss this part?
    >

    I don't think so. What might I have missed? Sarcasm? Maybe.
    >>>

    >> Wrong completely. 'if (argv[1] == "-help")' can never be expected to
    >> 'work' in your wildest dreams. argv[1] is a char* supplied by the
    >> command processor, "-help" is a char* to an anonymous array of char
    >> somewhere in memory. They will not ever be equal. Never.
    >>
    >> strcmp() is your friend.
    >>

    >
    >
    > boa



    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Feb 11, 2006
    #7
  8. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Alvin Guest

    *hits forhead*
    Should have thought of using that before!
    Thanks very much, Joe! I was worried I was going to get a stingy reply
    with no help at all.
    Alvin, Feb 11, 2006
    #8
  9. "Alvin" <> writes:
    > *hits forhead*
    > Should have thought of using that before!
    > Thanks very much, Joe! I was worried I was going to get a stingy reply
    > with no help at all.


    Should have thought of using what before?

    Don't assume we can easily see the article to which you're replying.
    Please read <http://cfaj.freeshell.org/google/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 11, 2006
    #9
  10. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    boa Guest

    Joe Wright wrote:
    > boa wrote:
    >> Joe Wright wrote:
    >>
    >>> Alvin wrote:
    >>> [ much snippage ]
    >>>
    >>>>
    >>>> One problem I've run into is this: when testing for an argument, say:
    >>>> if(argv[1] == "-help")
    >>>> in your case, it doesn't work. If you try:
    >>>> ---
    >>>> if(argv[1] == "-help")
    >>>> printf("\n<help>");
    >>>> else
    >>>> printf("\nyou typed %s", argv[1]);
    >>>> ---
    >>>> then no matter what, it drops to the else statment.

    >>
    >> -------^^^^^^^^^^^^^^^^
    >> Did you miss this part?
    >>

    > I don't think so. What might I have missed? Sarcasm? Maybe.



    My mistake. I messed up and for some reason thought that you responded
    to someone other than the OP, and thought that you didn't "get" the
    (rather poor) example of why one cannot compare string values like that
    in C.

    Sorry about that.
    boa
    [snip]
    boa, Feb 11, 2006
    #10
  11. stathis gotsis wrote:
    -snip-

    > Maybe this is relevant:
    > http://c-faq.com/misc/argv.html


    Thanks. That looks exactly like the place to start. Also thanks to the
    other answers. I assume argc contains the number of (space-separated)
    arguments.


    Med venlig hilsen / Best regards
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 11, 2006
    #11
  12. Martin Jørgensen <> writes:
    > stathis gotsis wrote:
    > -snip-
    >
    >> Maybe this is relevant:
    >> http://c-faq.com/misc/argv.html

    >
    > Thanks. That looks exactly like the place to start. Also thanks to the
    > other answers. I assume argc contains the number of (space-separated)
    > arguments.


    The arguments aren't necessarily space-separated; that depends on the
    mechanism (for example, a shell) used to invoke your program.

    argc is the number of valid elements of the array pointed to by argv.
    If you invoke your program with 2 command-line arguments, argc will be 3.
    argv[0] probably points to (some form of) the name of your program;
    argv[1] and argv[2] point to your command-line arguments.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 11, 2006
    #12
  13. On 11 Feb 2006 11:21:59 -0800, in comp.lang.c , "Alvin"
    <> wrote:

    >One problem I've run into is this: when testing for an argument, say:
    >if(argv[1] == "-help")
    >in your case, it doesn't work.


    you can't compare strings like that. This is why strcmp and its
    friends exist.,
    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Feb 12, 2006
    #13
  14. On Sat, 11 Feb 2006 21:08:53 +0100, in comp.lang.c , boa
    <> wrote:

    >Joe Wright wrote:
    >> Alvin wrote:
    >> [ much snippage ]
    >>>
    >>> One problem I've run into is this: when testing for an argument, say:
    >>> if(argv[1] == "-help")
    >>> in your case, it doesn't work. If you try:
    >>> ---
    >>> if(argv[1] == "-help")
    >>> printf("\n<help>");
    >>> else
    >>> printf("\nyou typed %s", argv[1]);
    >>> ---
    >>> then no matter what, it drops to the else statment.

    >-------^^^^^^^^^^^^^^^^
    >Did you miss this part?


    No, he didn't. Its meaningless to compare strings using ==, unless
    both are the same object it will almost inevitably fail (all you're
    doing is comparing the pointers). So naturally it drops down to the
    else.
    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Feb 12, 2006
    #14
  15. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    pete Guest

    Martin Jørgensen wrote:

    > I suspect that one should change the program such that "int main(??
    > something goes in here, right?)" instead of int main(void),
    > but I'm not
    > really sure of how to address this problem. If somebody has any sample
    > code in C to post, I would be very happy.


    /* BEGIN type_.c */
    /*
    ** This is a demonstration of a way to use fscanf
    ** to read lines from text files.
    */
    #include <stdio.h>

    #define ARGV_0 type_
    #define LINE_LEN 250
    #define str(s) # s
    #define xstr(s) str(s)

    int main(int argc, char *argv[])
    {
    int rc;
    FILE *fd;
    char line[LINE_LEN + 1];

    if (argc > 1) {
    while (*++argv != NULL) {
    fd = fopen(*argv, "r");
    if (fd != NULL) {
    do {
    rc = fscanf(fd,
    "%" xstr(LINE_LEN) "[^\n]%*[^\n]", line);
    if (!feof(fd)) {
    getc(fd);
    }
    if (rc == 0) {
    *line = '\0';
    }
    if (rc != EOF) {
    puts(line);
    }
    } while (rc == 1 || rc == 0);
    fclose(fd);
    } else {
    fprintf(stderr,
    "\nfopen() problem with \"%s\"\n", *argv);
    break;
    }
    }
    } else {
    puts(
    "Usage:\n>" xstr(ARGV_0)
    " <FILE_0.txt> <FILE_1.txt> <FILE_2.txt> ...\n"
    );
    }
    return 0;
    }

    /* END type_.c */


    --
    pete
    pete, Feb 12, 2006
    #15
  16. Keith Thompson wrote:
    > Martin Jørgensen <> writes:
    >
    >>stathis gotsis wrote:
    >>-snip-
    >>
    >>
    >>>Maybe this is relevant:
    >>>http://c-faq.com/misc/argv.html

    >>
    >>Thanks. That looks exactly like the place to start. Also thanks to the
    >>other answers. I assume argc contains the number of (space-separated)
    >>arguments.

    >
    >
    > The arguments aren't necessarily space-separated; that depends on the
    > mechanism (for example, a shell) used to invoke your program.


    If the arguments isn't space-separated, how else would they be separated?

    > argc is the number of valid elements of the array pointed to by argv.
    > If you invoke your program with 2 command-line arguments, argc will be 3.
    > argv[0] probably points to (some form of) the name of your program;
    > argv[1] and argv[2] point to your command-line arguments.


    Just as I thought.


    Med venlig hilsen / Best regards
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 12, 2006
    #16
  17. pete wrote:
    -snip-
    > /* END type_.c */


    Thanks for the example. Looks a little advanced but I'll try it out...


    Med venlig hilsen / Best regards
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 12, 2006
    #17
  18. "Martin Jørgensen" <> wrote in message
    news:...
    > Keith Thompson wrote:
    > > The arguments aren't necessarily space-separated; that depends on the
    > > mechanism (for example, a shell) used to invoke your program.

    >
    > If the arguments isn't space-separated, how else would they be separated?


    I've encountered two variations:

    program argument1,argument2,argument with space,,argument 5

    program/argument1/argument2/argument with space//argument 5

    These both got argc == 6 with

    argv[0] = "" (neither of them gave the program name in any form)
    argv[1] = "argument1"
    argv[2] = "argument2"
    argv[3] = "argument with space"
    argv[4] = ""
    argv[5] = "argument 5"

    The touted advantage of both was that you could have spaces, or even empties,
    in the list. One of them discarded leading and trailing blanks. The other
    didn't. I don't recall which now, but for the one that didn't, committing the
    typo of

    program argument1, argument 2

    got you

    argv[1] = "argument1"
    argv[2] = " argument 2"

    I was targeting both of these at the same time, so I just coded to remove
    leading and trailing blanks no matter what.

    - Bill
    William J. Leary Jr., Feb 12, 2006
    #18
  19. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    pete Guest

    Martin Jørgensen wrote:
    >
    > pete wrote:
    > -snip-
    > > /* END type_.c */

    >
    > Thanks for the example. Looks a little advanced but I'll try it out...


    The command line agurment part
    isn't especially advanced, I don't think.

    The fprintf part *is* a little bit advanced.

    --
    pete
    pete, Feb 12, 2006
    #19
  20. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    pete Guest

    pete wrote:
    >
    > Martin Jørgensen wrote:
    > >
    > > pete wrote:
    > > -snip-
    > > > /* END type_.c */

    > >
    > > Thanks for the example.
    > > Looks a little advanced but I'll try it out...

    >
    > The command line agurment part
    > isn't especially advanced, I don't think.


    But I should explain it anyway.

    For this form of main: int main(int argc, char *argv[]);

    argc is the number of command line arguments.
    argv is the NULL terminated array of pointers to strings,
    where the strings are command line arguments.
    The name of the program is the first argument.

    A system need not support command line arguments at all,
    in which case arc may be equal to zero,
    this something that should be checked for.
    If command line arguments are supported,
    then the first one will be the name of the program.

    --
    pete
    pete, Feb 12, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    10
    Views:
    656
    sushant
    Jan 20, 2005
  2. sapsi
    Replies:
    4
    Views:
    1,309
    Robert Kern
    Apr 21, 2009
  3. Iñaki Baz Castillo
    Replies:
    2
    Views:
    499
    Iñaki Baz Castillo
    Dec 23, 2009
  4. reeddeer
    Replies:
    5
    Views:
    192
    Martin Kissner
    Mar 13, 2005
  5. Santosh Kumar
    Replies:
    2
    Views:
    185
    Steven D'Aprano
    Sep 16, 2012
Loading...

Share This Page