title: I'm trying to create a string with n characters by allocating memories with malloc, but I have a problem
body: <pre class="lang-c prettyprint-override"><code>
      #define _CRT_SECURE_NO_WARNINGS
      #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
      
      int main(void)
      {
          int n;
          printf(&quot;Length? &quot;);
          scanf(&quot;%d&quot;, &amp;n);
          getchar();
          char* str = (char*)malloc(sizeof(char) * (n+1));
          fgets(str,sizeof(str),stdin);
          for (int i = 0; i &lt; n; i++)
              printf(&quot;%c\n&quot;, str[i]);
          free(str);
      }
  </code></pre>
  <p>Process results like this!</p>
  <p>Length? 5<br />
  abcde<br />
  a<br />
  b<br />
  c</p>
  <p>?</p>
  <p>(I wanted to upload the result image, but I got rejected since I didn't have 10 reputations)</p>
  <p>I can't figure out why 'd' and 'e' won't be showing in the results.
  What is the problem with my code??</p>