title: How to stop enter character getting stuck in buffer? C
body: <p>So in the following bit of code I'm reading an option from the user to then decide whether to perform a given action:</p>
  
  <pre><code>printf("Do you want to execute %s: ", exe);
  
  char input = getchar();
  
  if (input == 'y') {
      return execute(exe);
  } return 0;
  </code></pre>
  
  <p>The problem I'm having is that after the line <code>char input = getchar()</code> a character (I'm assuming an enter key or newline) gets stuck in <code>stdin</code>, and a future call to <code>fgets()</code> (reading from <code>stdin</code>) from my <code>execute()</code> function therefore eats up this stuck character and doesn't prompt the user for the required input. </p>
  
  <p>Now I can define the following function to 'eat' any stray characters for me if called before calling <code>execute()</code>...</p>
  
  <pre><code>void iflush() {
      int c;
  
      while ((c = getchar()) != EOF &amp;&amp; c != '\n') {
          continue;
      }
  }
  </code></pre>
  
  <p>... and this solves the problem. But I'm left wondering why this is happening in the first place? Is there a more 'proper' way of getting a <code>char</code> from <code>stdin</code> without causing this stray character which then messes up my other methods? I'm very new to C and am keen to be sure I'm learning good habits.</p>
  
  <p>It seems odd that I have to add a helper method, even if simple, to prevent errors in my program just from recording some user input. I've tried using <code>fgetc()</code> also but same problem.</p>