title: How to delete garbage characters from input in C
body: <p>I have a piece a code that works for input validation, I need a little bit more explanation for why it works. The line where the garbage variable reads a character from getchar and checks to see if it's not equal EOF confuses me a little. It's purpose is to read the garbage input from the user, and allow the user to re-enter the input. My question is, how exactly does this line work?</p>
  
  <pre><code>  int steps = 0;                                                
    int true;                                                 
    int garbage;                                              
  
  printf("Enter the integer increment number in the range ");  
  printf("of[%d - %d]: ", minInput, maxInput);
  true = scanf("%d", &amp;steps);
  
  /* INPUT VALIDATION */
  
  while (!true || isdigit(steps) ||(steps &lt; minInput || steps &gt; maxInput) ){  
      while ((garbage = getchar()) != EOF &amp;&amp; garbage != '\n') {                   
      } // end while;
  
      printf("Invalid input... please enter a number in the range ");
      printf("of[%d - %d]: ", minInput, maxInput);
      true = scanf("%d", &amp;steps);
  
  }// end while
  </code></pre>