title: How to remove extra characters input from fgets in C?
body: <p>I heard using gets() is bad in C programming and it's safer using fgets... So I am using fgets. However, I encounter a problem with fgets: I entered too much characters and somehow, it overflows.</p>
  
  <p>How to get rid of the extra input characters?</p>
  
  <pre><code>char answer[4];
  char answer2[4];
  
  fgets(answer,sizeof(answer),stdin);
  printf("answer: %s\n",answer);
  
  fgets(answer2,sizeof(answer2),stdin);
  printf("answer2: %s\n",answer2);
  </code></pre>
  
  <p>For example, for the first fgets, I enter 123456, the output I get is</p>
  
  <pre><code>answer: 123
  answer2: 456
  </code></pre>
  
  <p>How do I remove the 456 from going into the next fgets input? I want the output like this after entering 123456 for the 1st fgets:</p>
  
  <pre><code>answer: 123
  </code></pre>
  
  <p>Then, user continue to enter an input for the next fgets...</p>