Thread: "Karl's *ARGV[] Example" by Karl

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    41

    Thumbs up "Karl's *ARGV[] Example" by Karl

    This for people who want to know what to do with 'int main(int argc, char *argv[])', I'm open to suggustions or corrections.

    THX

    So here it is!

    Code:
    //////////////////////////////
    //  Karl's *ARGV[] Example
    //         argv.c
    //////////////////////////////
    //
    // This program prints the
    // command line.
    // 
    // If you use DOS, compile this
    // then type in 'argv', then run
    // this program then you will see
    // something like this:
    // 
    // Command Line:
    // C:\stuff\argv.exe
    // 
    // If you use Windows, compile this
    // then goto the start menu then
    // goto 'programs' and click on
    // 'MS-DOS Prompt', then type in:
    // C:\stuff\argv
    // Then you will see something like
    // this:
    // 
    // Command Line:
    // C:\stuff\argv.exe 
    // 
    // The Comments though-out the
    // program will explain more
    // about how this works.
    // 
    //////////////////////////////
    
    #include <stdio.h> // Standard I/O
    
    int main(int argc, char *argv[]) // *argv[] is a pointer that points to the strings on the command line
    {
      int ctr; // Counter Variable
      
      printf("Command Line:\n");
      for(ctr=0;ctr<argc;ctr++) // 'argc' is the number of strings on the command line
      {
        printf("%s\n", argv[ctr]);
      }
      
      getchar();
      return 0;
    }

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: "Karl's *ARGV[] Example" by Karl

    Originally posted by Budgiekarl

    printf("%s\n", argv[ctr]);
    I'd use the following printf statement to display a little more information:
    printf("argv[%d] = '%s' \n", cnt, argv[cnt]);

    Otherwise, quite useful. I've used this type of program for years.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed