arguments to main

This is a discussion on arguments to main within the C Programming forums, part of the General Programming Boards category; So I'm trying to learn about arguments to main. The idea here was to start the program with it's name ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Seattle, WA
    Posts
    5

    arguments to main

    So I'm trying to learn about arguments to main. The idea here was to start the program with it's name and then type in your first name. So in this case a.out Ryan. This is the output of the program, followed by the code. For some reason it is missing a section of the program.

    The value of name_counter variable is 1
    The value of name_counter variable is 2
    The value of name_counter variable is 3
    The value of name_counter variable is 4
    This line printed, did it output if name was short/long first?

    Code:
    #include <stdio.h>
    
    int main (int argc, char** argv)
    {
            /* VARIABLE DECLARATIONS */
    
            int i = 0, name_counter = 0;
    
            /* PROCESS FIRST NAME */
    
            do
            {
    
                    if ( argv[1][i] != '\0' )
                            {
                                    name_counter++;
                                    i++;
                                    printf("The value of name_counter variable is %i\n", name_counter); /* debugging line */
                            }
                    else if ( argv[1][i] == '\0' )
                            {
                                    printf("The program iterated thru the string and hit the '\0'"); /* debugging line */
                                    if ( name_counter <= 3 )
                                            printf("That's a short first name!");
                                    else if ( name_counter >= 3 )
                                            printf("That's a long first name!");
                                    else
                                            printf("There is an error in the 'else if ( argv[1][i] == '\0' ) routine'");
                            }
                    else
                            printf("The outer if/else in the name counting routine is not functioning properly.");
            }
            while ( argv[1][i] != '\0' );
    
            printf("This line printed, did it output if name was short/long first?\n\n"); /* debugging line */
    
            return 0;
    
    } /* end main */

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Simply put, argv is an array of pointers to strings. As you may, or may not know, you can get the length of a string with strlen() from string.h, i.e.:

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main(int argc, char * argv[])
    {
       char * firstname = NULL;
       size_t length = 0;
    
       /* sanity checking */
       if(argc < 2)
       {
          printf("Usage: %s <firstname>\n", argv[0]);
          return 1;
       }
    
       /* ease of reading ;) */
       firstname = argv[1];
       length = strlen(firstname);
    
       if(length <= 3)
       {
          printf("That's a short firstname!\n");
    
       /* > 3 */
       }else{
          printf("That's a long firstname!\n");
       }
    
       return 0;
    }
    You need to brush up on your logic:
    Code:
       if ( name_counter <= 3 )
          printf("That's a short first name!");
       else if ( name_counter >= 3 )
          printf("That's a long first name!");
       else
          printf("There is an error in the 'else if ( argv[1][i] == '\0' ) routine'");
    That last "else" can NEVER execute.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The "while (argv[1][i] != '\0')" part guarantees that your short/long first name will never happen, since your loop must necessarily break when argv[1] hits the \0 character.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    The most civilized method is to use the industry standard getopt() calls:
    Example of Getopt - The GNU C Library
    Code:
    // looks like this:
    #include <ctype.h>
         #include <stdio.h>
         #include <stdlib.h>
         #include <unistd.h>
         
         int
         main (int argc, char **argv)
         {
           int aflag = 0;
           int bflag = 0;
           char *cvalue = NULL;
           int index;
           int c;
         
           opterr = 0;
         
           while ((c = getopt (argc, argv, "abc:")) != -1)
             switch (c)
               {
               case 'a':
                 aflag = 1;
                 break;
               case 'b':
                 bflag = 1;
                 break;
               case 'c':
                 cvalue = optarg;
                 break;
               case '?':
                 if (optopt == 'c')
                   fprintf (stderr, "Option -%c requires an argument.\n", optopt);
                 else if (isprint (optopt))
                   fprintf (stderr, "Unknown option `-%c'.\n", optopt);
                 else
                   fprintf (stderr,
                            "Unknown option character `\\x%x'.\n",
                            optopt);
                 return 1;
               default:
                 abort ();
               }
         
           printf ("aflag = %d, bflag = %d, cvalue = %s\n",
                   aflag, bflag, cvalue);
         
           for (index = optind; index < argc; index++)
             printf ("Non-option argument %s\n", argv[index]);
           return 0;
         }
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I don't see how getopt() helps at all in this circumstance, he's not trying to parse options. The use of command-line arguments does not automatically warrant the use of getopt()...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many arguments to function int main?
    By plain in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2006, 07:01 PM
  2. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 07:54 AM
  3. Passing arguments to main?
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2003, 04:59 AM
  4. arguments passed to main?!?
    By threahdead in forum C++ Programming
    Replies: 14
    Last Post: 01-22-2003, 08:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21