[newbie] about command line arguments

Discussion in 'C Programming' started by iceBlue, Jun 26, 2003.

  1. iceBlue

    iceBlue Guest

    Hello all

    I was answering a 'Quiz on C' when i stumbled upon these questions on
    CL-Arguments.I know the questions are stupid and wont be needed for any
    serious coding..but iam curious to know the answers :)

    *What is the maximum combined length of command line arguments including the
    space between adjacent arguments?

    *Are the variables argc and argv local to main?

    *If we want that any wildcard characters in the command line arguments
    should be appropriately expanded, are we required to make any special
    provision? If yes, which?

    *Does there exist any way to make the command line arguments available to
    other functions without passing them as arguments to the function?

    iceBlue, Jun 26, 2003
    1. Advertisements

  2. Implementation-dependent, I should guess.
    Yes. *All* function parameters are local to that function.
    I don't understand what you mean by "any special provision", but I think
    that yes you have to do it. C has no knowledge of "wildcards". You'll
    have to implement the entire expansion algorithm yourself or use a
    third-party solution.
    Store them in global variables.

    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "The large yellow ships hung in the sky in exactly the same way that bricks
    - Douglas Adams
    Joona I Palaste, Jun 26, 2003
    1. Advertisements

  3. iceBlue

    Materialised Guest

    This wreaks of homework assignment!


    Yahoo Messenger: Materialised
    MSN Messenger: Walker123412 at hotmail dot com
    GnomeMeeting: Materialised at ntlworld dot com

    Materialised, Jun 26, 2003
  4. Whether your answer is true depends on your definition of "command line
    arguments". Are those those that the user types from his shell, or those
    that the C program ends up receiving?
    Note that even in Linux, it's possible to say something like
    "./mygroovyprogram \*" and then argv[1] will be the string "*". You can
    then expand it by hand if you want to. No one is forcing you to expand
    it to every file (and directory) in the current directory. You can
    expand it to the names of all people who live in Paris, for example.

    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "Nothing lasts forever - so why not destroy it now?"
    - Quake
    Joona I Palaste, Jun 26, 2003
  5. iceBlue

    Eric Sosman Guest

    Since it's a "Quiz on C" I'll provide C answers.
    Implementation-dependent; C imposes no maximum and requires
    no minimum. (And as far as I can tell, the implementation is
    not required to document whatever limits it may impose.)
    No. For example, consider this program:

    double argc;
    int argv;
    int main(int x, char**y) { return 0; }

    Clearly, the variables argc and argv are not local to main().
    Yes, special provisions are required. One such might be
    to invoke the C program from a helper program (sometimes called
    a "shell") that takes care of the expansion.
    Yes. For example,

    #include <stdio.h>
    char **argv_copy;

    int func(void) {
    return *argv_copy > 0;

    int main(int argc, char **argv) {
    argv_copy = argv;
    if (func) puts(argv[0]);
    return 0;
    Eric Sosman, Jun 26, 2003
  6. Crap. I missed that in my original reply. I am so used to naming the
    parameters argc and argv I immediately associated the names to main()'s
    parameters. Now I feel foolish.

    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
    - ALF
    Joona I Palaste, Jun 26, 2003
  7. iceBlue

    Eric Sosman Guest

    I've often thought that

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

    .... would make a good beginning for an IOCCC entry.
    Eric Sosman, Jun 26, 2003
  8. It depends on the system. Under linux wild cards and otrher such stuff
    is handled by the shell..I think the same goes for windows and dos
    though the functionality is different. A guess from me would say that in
    the context of the quiz the answer is no. The C answer is that main
    takes whatever is given to it and if whatever calls main does not handle
    it you will have to.
    Thomas Stegen, Jun 26, 2003
  9. This would be implementation defined. It is not really about C language.
    It is about the command line environment you are running a program in.
    Assuming you define main as:

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

    then yes, they are local to main. Something the person creating the
    questions might not have consider would be:

    int main(int weird, char *difference[])

    is perfectly valid. This means, if argc and argv exist they don't have to
    be local to main. Additionally, there is nothing saying there is only one
    instance of argc and argv. I could have:

    void func(void)
    int argc, argv;
    /* stuff */

    as well as the usual main function.
    This is all implementation defined/command line environment specific
    information. Just like the first question, this is not really about C
    Create global variables and assign the argc and argv variables to those
    global variables.
    Darrell Grainger, Jun 26, 2003
  10. iceBlue

    Alan Balmer Guest

    Without knowing the context of the quiz, I wouldn't try to guess. In
    DOS (and probably Windows, though I'm not sure) the standard shell
    does not expand wildcards, and many C implementations provided a
    library function to do it.
    Alan Balmer, Jun 26, 2003
  11. If this is homework they should consider a different course. These are
    some pretty useless questions.
    Darrell Grainger, Jun 26, 2003
  12. I was answering a 'Quiz on C' when i stumbled upon these questions on
    //STEPNAME EXEC PGM=CC,PARM='-o,xyz,xyz.c'

    *WHAT* spaces between adjacent arguments?

    Nobody said there is a limit on the length of command-line arguments
    including the space. The limit might be, for example, $25.00 and
    100 yen, each argument costing $1.00 and 1 yen, and each character
    in each argument NOT including the space costing $0.01, except
    quotes, which cost $0.05 and 2 yen. You are NOT allowed to exchange
    yen for dollars or vice versa.
    Assuming you mean the arguments to main(), yes.
    ANSI C makes no guarantees that there are wildcards. If there are,
    it's up to whatever you use to invoke the program. For example,
    the UNIX exec() function doesn't expand wildcards. Most UNIX shells
    ANSI C doesn't guarantee that division by zero WON'T do this.
    There is no standard way to do this.

    Gordon L. Burditt
    Gordon Burditt, Jun 26, 2003
  13. iceBlue

    Russ Bobbitt Guest

    I've maintained some code where the programmer did exactly that. :-/
    Russ Bobbitt, Jun 27, 2003
  14. iceBlue

    Micah Cowan Guest

    There is no answer for this. In C, command line arguments don't have
    "space between them" -- they are passed via a vector of pointers-to-char.
    "Appropriately expanded"? Generally, this is expected of the command
    shell, not of the program.
    You can of course store their values in globals, but I'm not crazy
    about that.

    Micah Cowan, Jun 27, 2003
  15. iceBlue

    Dan Pop Guest

    Nope. On DOS implementations, it is the C runtime that does (or does not)
    expand them (you can usually control this behaviour via a global
    variable or by linking a certain object module to your application).
    The problem is that we don't know the context of the quiz. Is it about
    Unix programming, DOS programming, a certain implementation? Some of the
    questions simply make no sense in an implementation-independent context...

    Dan Pop, Jun 27, 2003
  16. iceBlue

    Dan Pop Guest

    Agreed. Especially considering that some of the answers are highly
    platform specific.

    Dan Pop, Jun 27, 2003
  17. iceBlue

    Dan Pop Guest

    They usually have null characters between them :)

    Dan Pop, Jun 27, 2003
  18. Groovy hepcat iceBlue was jivin' on 26 Jun 2003 17:30:50 GMT in
    [newbie] about command line arguments's a cool scene! Dig it!
    And what will you give us for doing your homework for you?
    Mu. There is no space between adjacent command line arguments, as
    far as C is concerned.
    Mu. What are argc and argv? It is comman practice to call main()'s
    parameters argc and argv respectively. But this is not universal. Nor
    is it unheard of to give other variables or function parameters those
    names. You could even have macros or functions with those names.
    Mu. What your program understands to be a wildcard character is
    entirely up to your program, as is the action taken when such a
    character is encountered. As far as C is concerned, there is no such
    thing as a wildcard character.
    Oh, really! Can't you fire up one lousy brain cell and answer this
    one yourself? It's ludicrously simple. (Although, I don't generally
    approve of the solution. But that's beside the point.)


    Dig the even newer still, yet more improved, sig!

    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Jun 30, 2003
  19. iceBlue

    Richard Bos Guest

    main() is (almost) an ordinary function. Because of this, it does not
    matter a whit, to the compiler, what you name its arguments. It is
    customary to define main() as

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

    or similar when one uses command line arguments. Most people stick to
    this custom, and that is a good thing, because it keeps down confusion.
    However, _from the implementation's POV_, it is completely immaterial
    what the names of those parameters are, as long as you use them
    consistently. Thus,

    int main(int number_of_arguments, char **text_of_arguments)

    is just as legal as the canonical definition. So is

    int main(int x, char **y)

    and so is Russ' definition. Compare his definition to the one I gave.
    Closely. Legal, but very, very unwise...

    Richard Bos, Jun 30, 2003
  20. iceBlue

    Zoran Cutura Guest

    Here is an unbfuscated version of this code:
    #include <stdio.h>

    int func(void);

    int main(const int argc, char **argv)
    static st_argc;
    static char** st_argv;

    if (st_argv == 0) { /* at startup remember argc and argv */
    st_argc = argc;
    st_argv = argv;
    } else {

    /* assume that am called from within program! */
    if (argv == 0) /* initially called back from function */
    return st_argc; /* return initial argc */

    if (argc > 0) {
    *argv = st_argv[argc-1];

    return 0;


    /* here do all the other stuff main needs to do */

    return 0;

    int func(void)
    int main_argc, i;
    char *nextarg;

    main_argc = main(0, 0); /* initial call needs to pass 0 in argv!! */

    for(i = 0; i < main_argc; i++) {
    main(i+1, nextarg);
    puts(nextarg); /* here is where the arguments can be used! */

    return 0;

    I think it assumes that argc cannot be 0 which is not guarenteed by the
    standard, but it should be easy to add a check for that.
    Zoran Cutura, Jul 4, 2003
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.