title: Properly parse command line arguments in C
body: <p>What I am trying to do is take in command line arguments and change some variables according to the arguments. I have attached a chunk of my code because the whole code is ~400 lines. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  int main(int argc, char *argv[]) {
  
      char somestring[500];
      int ca=0;
      if (argc==1) //if no arguments are specified use defaults
      {
      }
      else
      {
          while(ca&lt;argc)
          {
                 ca++
              if(strcmp(argv[ca],"-f")==0)
              {
                  printf("This works");
                  ca++; 
                  if(strcmp(argv[ca],"red")==0){
                      printf("this will print red\n");
                  }
                  else{
                      printf("invalid color");
                  }
              }
              if(strcmp(argv[ca),"")==0)
              {
                  printf("invalid argument");
              }
              else {
                  strcat(somestring,argv[ca]);
              }
          }
          printf("%s",somestring);
      }
  }
  </code></pre>
  
  <p>If the user inputs:</p>
  
  <blockquote>
    <p>./foobar -f red this is a string</p>
  </blockquote>
  
  <p>the program should print:</p>
  
  <blockquote>
    <p>"this will print red this is a string"</p>
  </blockquote>
  
  <p>If the user inputs:</p>
  
  <blockquote>
    <p>./foobar -f red</p>
  </blockquote>
  
  <p>the program should print "invalid number of command line arguments".</p>
  
  <p>What is the easiest way to do this? I have tried tons of possibilities with no luck.
  Varying number of arguments is the main problem for me (also I have more than 5 options e.g..-f -b -h -w -e)</p>
  
  <p>Help would much appreciated. I can add my whole code if you want.</p>