title: How can I clear stdin in a C progam?
body: <p>This is a seemingly simple question that I have not been able to answer for far too long:</p>
  
  <p>I am trying to read input from a user in a C program using fgets(). However, I am running into the problem that if the user enters more characters than fgets() is set to read, the next call to read a string from the user automatically reads the remaining characters in the stdin buffer, and this is NOT behavior I want. </p>
  
  <p>I have tried many ways to clear the stdin stream, and while I know something like</p>
  
  <p><code>while(getchar()!='\n');</code>
  will work, this requires the user to hit enter an additional time which is not something I want.  </p>
  
  <p>The structure of the code looks something like this:</p>
  
  <pre><code>    void read_string(char *s, int width){
          fgets(s,width,stdin);
          clear_stdin();
          .
          .   
      }
  
      while (1){
          read_string()
          .
          .
      }
  </code></pre>
  
  <p>But I cannot get a clear_stdin() function that works desirably. How on earth can I clear the stdin, without having the user needlessly need to hit enter twice?</p>