title: dynamically allocating memory to save user input in it
body: <p>I'm trying to write this simple code which takes the user input message, saves it on the stack, and then shows it back to the user.</p>
  <p>I don't want to limit the number of characters the user can type in, so I used dynamic memory allocation each time the user enters a new character.</p>
  <p><strong>The code runs well if the user entered a small no. of characters, but it doesn't work if the user typed in a big no. of characters</strong></p>
  <p>for example: if I entered &quot;Ahmed&quot; it will show it back to me, but if I typed something with more characters it doesn't.</p>
  <p>this is my code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      char *UserInput;
      UserInput=(char *)calloc(1,sizeof(char));
      int i=0,ii=0;
      printf(&quot;Enter a message! \n&quot;);
      while(*(UserInput+ii)!='\n'){
          scanf(&quot;%c&quot;,(UserInput+i));
          ii=i;
          i++;
          UserInput=realloc(UserInput,i*sizeof(char));
      }
      for(i=0;i&lt;=ii;i++){
          printf(&quot;%c&quot;,*(UserInput+i));
      }
  
      return 0;
  }
  </code></pre>