Search code examples
clinuxarguments

Pass arguments into C program from command line


So I'm in Linux and I want to have a program accept arguments when you execute it from the command line.

For example,

./myprogram 42 -b -s

So then the program would store that number 42 as an int and execute certain parts of code depending on what arguments it gets like -b or -s.


Solution

  • You could use getopt.

     #include <ctype.h>
     #include <stdio.h>
     #include <stdlib.h>
     #include <unistd.h>
     
     int
     main (int argc, char **argv)
     {
       int bflag = 0;
       int sflag = 0;
       int index;
       int c;
     
       opterr = 0;
     
       while ((c = getopt (argc, argv, "bs")) != -1)
         switch (c)
           {
           case 'b':
             bflag = 1;
             break;
           case 's':
             sflag = 1;
             break;
           case '?':
             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 ("bflag = %d, sflag = %d\n", bflag, sflag);
     
       for (index = optind; index < argc; index++)
         printf ("Non-option argument %s\n", argv[index]);
       return 0;
     }