title: Flushing stdin after every input - which approach is not buggy?
body: <p>After <code>Mark Lakata</code> pointed out that the garbage isn't properly defined in my question I came up with this. I'll keep this updated to avoid confusions.</p>
  
  <hr>
  
  <p>I am trying to get a function that I can call  before a prompt for user input like <code>printf("Enter your choice:);</code> followed a <code>scanf</code> and be sure that only the things entered after the prompt would be scanned in by <code>scanf</code> as valid input. </p>
  
  <p>As far as I can understand the function that is needed is something that flushes standard input completely. That is what I want. So for the purpose of this function the <code>"garbage"</code> is everything in user input i.e. the whole user input before that user prompt.</p>
  
  <hr>
  
  <p>While using <code>scanf()</code> in C there is always the problem of extra input lying in the input buffer. So I was looking for a function that I call after every scanf call to remedy this problem. I used <a href="http://www.cplusplus.com/reference/cstdio/scanf/" rel="nofollow noreferrer">this</a>, <a href="https://stackoverflow.com/a/3876183/2235567">this</a>, <a href="https://stackoverflow.com/questions/3876091/c-flushing-stdin#comment4126074_3876183">this</a> and <a href="http://c-faq.com/stdio/stdinflush2.html" rel="nofollow noreferrer">this</a> to get these answers</p>
  
  <pre><code>//First approach
  scanf("%*[^\n]\n");
  
  //2ndapproach
  scanf("%*[^\n]%*c");
  
  //3rd approach
  int c;
  while((c = getchar()) != EOF) 
      if (c == '\n') 
          break;
  </code></pre>
  
  <p>All three are working as far as I could find by hit-and-trial and going by the references. But before using any of these in all of my codes I wanted to know whether any of these have any bugs?</p>
  
  <p>EDIT:</p>
  
  <p>Thanks to <a href="https://stackoverflow.com/users/364818/mark-lakata">Mark Lakata</a> for one bug in 3rd. I corrected it in the question.</p>
  
  <p>EDIT2:</p>
  
  <p>After <a href="https://stackoverflow.com/users/179910/jerry-coffin">Jerry Coffin</a> answered I tested the 1st 2 approaches using this program in code:blocks IDE 12.11 using GNU GCC Compiler(Version not stated in the compiler settings).</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  int main()
  {
      int x = 3; //Some arbitrary value
      //1st one
      scanf("%*[^\n]\n");
      scanf("%d", &amp;x);
      printf("%d\n", x);
  
      x = 3;
      //2nd one
      scanf("%*[^\n]%*c");
      scanf("%d", &amp;x);
      printf("%d", x);
  }
  </code></pre>
  
  <p>I used the following 2 inputs</p>
  
  <p><strong>First Test Input</strong> (2 Newlines but no spaces in the middle of garbage input)</p>
  
  <pre><code>abhabdjasxd
  
  
  23
  bbhvdahdbkajdnalkalkd
  
  
  
  46
  </code></pre>
  
  <p>For the first I got the following output by the <code>printf</code> statements</p>
  
  <pre><code>23
  46
  </code></pre>
  
  <p>i.e. both codes worked properly.</p>
  
  <p><strong>Second Test input</strong>: (2 Newlines with spaces in the middle of garbage input)</p>
  
  <pre><code>hahasjbas asasadlk
  
  
  23
  manbdjas sadjadja a
  
  
  46
  </code></pre>
  
  <p>For the second I got the following output by the <code>printf</code> statements</p>
  
  <pre><code>23
  3
  </code></pre>
  
  <p>Hence I found that the second one won't be taking care of extra garbage input whitespaces. Hence, it isn't foolproof against garbage input.</p>
  
  <p>I decided to try out a <strong>3rd test case</strong> (garbage includes newline before and after the non-whitespace character)</p>
  
  <pre><code>``
  hahasjbas asasadlk
  
  
  23
  
  manbdjas sadjadja a
  
  
  46
  </code></pre>
  
  <p>The answer was</p>
  
  <pre><code>3
  3
  </code></pre>
  
  <p>i.e. both failed in this test case.</p>