title: Please explain this behavior of fgets() in C
body: <p>To my understanding, <code>fgets()</code> will scan for input until a carriage return(<code>\n</code>), but my this code shows otherwise. I do not know where the extra line come from. Can someone explain?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define BUF_SZ 256
  
  void reverse(char str[]) {
      /* fill in the function body please */
      for (int i = 0, j = strlen(str) - 1; i &lt; strlen(str) / 2; i++, j--)
      {
          char temp; // perform the swap
          temp = str[i];
          str[i] = str[j];
          str[j] = temp;
      }
  }
  
  int main() {
      char mesg[BUF_SZ];
      printf("Enter a message please=&gt; ");
      fgets(mesg,BUF_SZ,stdin);
      reverse(mesg);
      printf("%s\n",mesg);
      return 0;
  }
  </code></pre>
  
  <p>This is the output:
  <a href="https://i.stack.imgur.com/RJSbG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RJSbG.png" alt="enter image description here"></a></p>