Re: Command line arguments

From: Richard Bos (rlb_at_hoekstra-uitgeverij.nl)
Date: 03/09/04


Date: Tue, 09 Mar 2004 10:56:13 GMT

Irrwahn Grausewitz <irrwahn33@freenet.de> wrote:

[ Slightly condensed... ]
> The following program prints all given command line arguments:
>
> #include <stdio.h>
>
> int main( int argc, char *argv[] ) /* [1] */
> {
> int i;
>
> for ( i = 0; i < argc; i++ )
> puts( argv[ i ] );
> return 0;
> }
>
> Notes:
> 1. argc holds the number of arguments
> 2. argc is guaranteed to be non-negative
> 3. argv is an array of pointers to strings holding
> the actual arguments (if argc > 0)
> 4. argv[ argc ] is guaranteed to equal NULL
> 5. if argc > 0, argv[ 0 ] may contain (part of) the path of
> the executable, or an empty string, depending on the
> implementation.

6. If your OS cannot give you both upper- and lower-case strings, you're
   guaranteed to get them in lower case.
7. argc and argv, being normal function arguments, can be modified (but
   then, of course, don't correspond to the CLA you gave); the pointer
   members of argv can _not_ be portably written to; but the character
   strings these pointers point to are, again, writable. I.e., argv=x
   is legal; argv[1]=x is not; but argv[1][1]=x is, providing that
   argv[1][1] does exist. Odd, but true.

Richard



Relevant Pages

  • Re: starting into H&S
    ... pointers. ... If argc is 3, and argv is null, he's not using a conforming ... There is an extra level of indirection here. ...
    (comp.lang.c)
  • Re: No more Foxpro
    ... your mainfunction gives you to paramters. ... The second, argv[] is an array ... of string pointers that point to the various paramters. ... I'd love to have an argc and argv. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: int main (int argc, char **argv)
    ... > I just wanted to say that the first element of argv is the path of the ... In the latter form argc shall be the number of arguments passed to the ... pointers to the initial characters of null-terminated multibyte strings ...
    (alt.comp.lang.learn.c-cpp)
  • Re: argc & argv
    ... I know that argv is an array of pointers to strings ... make sure the strings are properly built but my program crashes. ... void myMain(int argc, char** argv) ...
    (microsoft.public.vc.language)
  • Re: Help a beginner - simple lowercase to uppercase and so on function
    ... int main(int argc, char **argv)? ... Nothing is fundamentally wrong with modifying those, or the strings ... rare in Unix to modify argv which can, on someUnices, cause the "ps" ...
    (comp.lang.c)