title: About the mechanism of using fgets() and stdin together
body: <p>I would like to have a better understanding of using <code>fgets()</code> and <code>stdin</code>.
  The following is my code:</p>
  
  <pre><code>int main()
  {
      char inputBuff[6];
      while(fgets(inputBuff, 6, stdin))
      {
          printf("%s", inputBuff);
      }
      return 0;
  }
  </code></pre>
  
  <p>Let's say my input is <code>aaaabbbb</code> and I press Enter. By using a loopcount, I understand that actually the loop will run twice (including the one I input <code>aaaabbbb</code>) before my next input. </p>
  
  <p><strong>Loop 1:</strong> After I have typed in the characters, <code>aaaabbbb\n</code> will be stored in the buffer of <code>stdin</code> file stream. And <code>fgets()</code> is going to retrieve a specific number of data from the file stream and put them in <code>inputBuff</code>. In this case, it will retrieve 5 (6 - 1) characters at a time. So that when <code>fgets()</code> has already run once, <code>inputBuff</code> will store <code>aaaab</code>, and then be printed. </p>
  
  <p><strong>Loop 2:</strong> Then, since <code>bbb\n</code> are left in the file stream, <code>fgets()</code> will execute for the second time so that <code>inputBuff</code> contains <code>bbb\n</code>, and then be printed.</p>
  
  <p><strong>Loop 3:</strong> The program will ask for my input (the 2nd time) as the file stream has reached the end (<code>EOF</code>).</p>
  
  <p><strong>Question:</strong> It seems that <code>fgets()</code> will only ask for my keyboard input after <code>stdin</code> stream has no data left in buffer (<code>EOF</code>). I am just wondering why couldn't I use keyboard to input anything in loop 2, and <code>fgets()</code> just keep on retrieving 5 characters from <code>stdin</code> stream and left the excess data in the file stream for next time retrieval. Do I have any misunderstanding about <code>stdin</code> or <code>fgets()</code>? Thank you for your time!</p>