title: How does "optind" get assigned in C?
body: <p>I am creating this question because there is not much about how this optind gets assigned for each loop.</p>
  
  <p><a href="https://linux.die.net/man/3/getopt" rel="noreferrer">Man page</a> says :</p>
  
  <blockquote>
    <p>The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1.</p>
  </blockquote>
  
  <p>Below, I have a simple code I got from <em>Head First C</em> and in the code we <strong>subtract</strong> "<em>optind</em>" from "<em>argc</em>" and we get the number of leftover arguments, which will we use then to print leftover arguments as "Ingredients".</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  int main(int argc, char* argv[]) {
  char* delivery = "";
  int thick = 0 ;
  int count = 0;
  
  char ch;,
  
  for(int i = 0; i &lt; argc;i++){
  //This is , to show the whole array and their indexes.
      printf("Argv[%i] = %s\n", i, argv[i]);
  }
  while((ch = getopt(argc, argv, "d:t")) != -1 ){
      switch(ch) {
          case 'd':
              printf("Optind in case 'd' : %i\n",optind);
              delivery = optarg;
              break;
          case 't':
              printf("Optind in case 't' : %i\n",optind);
              thick = 1;
              break;
          default:
              fprintf(stderr,"Unknown option: '%s'\n", optarg); // optional argument.
              return 1;
      }
  }
      argc -= optind;
      argv += optind;
      printf("Optind : %i and Argc after the subctraction : %i\n",optind,argc);
      if(thick)
          puts("Thick crust");
      if(delivery[0]){
          printf("To be delivered %s\n", delivery);
      }
  
      puts("Ingredients:");
      for(count = 0; count &lt; argc ; count ++){
          puts(argv[count]);
      }
      return 0;
  }
  </code></pre>
  
  <p>So at the beginning of the code the for loop writes all the array and its indexes to see the difference.</p>
  
  <p>Then I run the code with :</p>
  
  <pre><code>./pizzaCode -d now Anchovies Pineapple -t //-t is intentionally at the end
  </code></pre>
  
  <p>I was told that if the flag was at the end it wouldn't get in the 't' case but somehow it works on my ubuntu. That is another thing I wonder but not the main question.</p>
  
  <p>So the output is as follows : </p>
  
  <pre><code>Argv[0] = ./pizzaCode
  Argv[1] = -d
  Argv[2] = now
  Argv[3] = Anchovies
  Argv[4] = Pineapple
  Argv[5] = -t
  Optind in case 'd' : 3
  Optind in case 't' : 6
  Optind : 4 and Argc after the subctraction : 2
  Thick crust
  To be delivered now
  Ingredients:
  Anchovies
  Pineapple
  </code></pre>
  
  <p>1- Everything is fine so far, the problem is how come argv[0] and argv<a href="https://linux.die.net/man/3/getopt" rel="noreferrer">1</a> became Anchovies and Pineapple ?</p>
  
  <p>2- And another question is how did optind become 3 in case 'd'? Since 'd's index is 1 and the next index is 2.</p>
  
  <p>3- How did optind become 4 after the loop ? It was 6 in the case 't'.</p>
  
  <p>I hope my question is clear for you all, I am just trying to understand the logic instead of having to memorize it.
  Thank you in advance!</p>