title: Unable to properly read integers from input via fgets()
body: <p>I am currently practicing reading input from the user, and I am using fgets to read the input, but there seems to be a problem.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  int main(int argc, char *argv[]){
      int N = 0;
      char ar[2];
      fgets(ar, 2, stdin);
      N = atoi(ar);
       char arr[N][22];
  
      // for loop automatically runs automatically
      for(int i = 0; i&lt; N; i++){ 
              printf("i is %d\n", i);
              fgets(arr[i], 22, stdin);
      }
      return 0;
  }
  </code></pre>
  
  <p>First, I read an integer using fgets and I employ atoi to convert it into an integer. Then using that integer, I loop N times, however for some reason it seems that there is some stuff still left in the input stream and it is causing my for loop to automatically run two times.</p>
  
  <p>EDIT:</p>
  
  <p><strong>input:</strong> </p>
  
  <p>5</p>
  
  <p><strong>output (automatically):</strong> </p>
  
  <p>i is 0</p>
  
  <p>i is 1</p>