title: I am not able to flush stdin. How can I flush stdin in C?
body: <p>How to <strong>flush the stdin</strong>??</p>
  <p>Why is it not working in the following code snippet?</p>
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;fcntl.h&gt;
  
  int main() {
      int i = 0, j = 0, sat;
      char arg[256];
      char *argq;
      argq = malloc(sizeof(char) * 10);
      
      printf(&quot;Input the line\n&quot;);
      i = read(0, arg, sizeof(char) * 9);
      arg[i - 1] = '\0';
      fflush(stdin);
      
      i = read(0, argq, sizeof(char) * 5);
      argq[i - 1] = '\0';
  
      puts(arg);
      puts(argq);
      
      return 0;
  }
  </code></pre>
  <p>Now if I give the input as 11 characters, only 9 should be read but the remaining two characters in the stdin are not flushed and read again in the argq. Why?</p>
  <p>Input: <code>123 456 789</code></p>
  <p>Output:</p>
  <pre><code>123 456
  89
  </code></pre>
  <p>Why am I getting this <code>89</code> as the output?</p>