title: Is there an elegant way to handle the '\n' that gets read by input functions (getchar(), fgets(), scanf()) in C?
body: <p>I am trying a simple exercise from K&amp;R to append <strong>string2</strong> at the end of <strong>string1</strong> using pointers. In case of overflow i.e. buffer of <strong>string1</strong> can't contain all of <strong>string2</strong> I want to prompt the user to re-enter <strong>string2</strong> or exit.</p>
  <p>I have written the following code:</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #define MAXLINE 1000
  int get_input(char *s);
  int str_cat(char *s, char *t);
  
  void main()
  {
      char input1[MAXLINE], input2[MAXLINE], c; 
      get_input(input1);
      check:
      get_input(input2);
      if((strlen(input1) + strlen(input2) + 2) &lt;= MAXLINE)
      {
          str_cat(input1, input2);
          printf(&quot;%s\n&quot;, input1);
      
      }
      else
      {
          input2[0] = '\0';
          printf(&quot;String overflow\n Press: \n 1: Re-enter string. \n 2: Exit.\n&quot;);
          scanf(&quot; %d&quot;, &amp;c);
          if(c == 1){
              input2[0] = '\0';
              get_input(input2);
              goto check;
          }
  
      }
  
  }
  
  int get_input(char *arr)
  {
      int c;
      printf(&quot;Enter the string: \n&quot;);
      while(fgets(arr, MAXLINE, stdin))
      {
          break;
      }
  }
  int str_cat(char *s, char *t)
  {
      while(*s != '\0')
      {
          s++;
      }
      while((*s++ = *t++) != '\0')
      {
          ;
      }
      *s = '\0';
  }
  </code></pre>
  <p>Initially, I was using the standard <code>getchar()</code> function mentioned in the book to read the input in <code>get_input()</code> which looked like this:</p>
  <pre><code>int get_input(char *arr)
  {
      int c;
      printf(&quot;Enter the string: \n&quot;);
      while((c = getchar()) != '\n' &amp;&amp; c != EOF)
      {
          *arr++ = c;
      }
      *arr = '\0';
  }
  </code></pre>
  <ul>
  <li><p>I am new and I read <a href="https://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c">this</a> and understood my mistake. I understand that one isn't supposed to use different input functions to read stdin and the <code>'\n'</code> is left in the input stream which is picked by the <code>getchar()</code> causing my condition to fail.</p>
  </li>
  <li><p>So, I decided to use <code>fgets()</code> to read the input and modified the <code>scanf(&quot;%d&quot;, &amp;c)</code> as mentioned in the thread with <code>scanf(&quot; %d&quot;, c)</code>. This does work (kinda) but gives rise to behaviors that I do not want.</p>
  </li>
  </ul>
  <p>So, I have a few questions:</p>
  <ol>
  <li><p>What's a better way to <code>fgets()</code> from reading the input on encountering <code>'\n'</code> than the one I have used?</p>
  <pre><code> while(fgets(arr, MAXLINE, stdin))
   {
       break;
   }
  </code></pre>
  </li>
  <li><p><code>fgets()</code> stops reading the line and stores it as an input once it either encounters a <code>'\n' or EOF</code>. But, it ends up storing the <code>'\n'</code> at the end of the string. Is there a way to prevent this or do I have to over-write the <code>'\n'</code> manually?</p>
  </li>
  <li><p>Even though I used the modified version of <code>scanf(&quot; %d&quot;, &amp;c)</code>, my output looks like
  this: (<a href="https://imgur.com/a/RaC2Kc6" rel="nofollow noreferrer">https://imgur.com/a/RaC2Kc6</a>). Despite that I get <code>Enter the string: </code> twice when prompted to re-enter the second string in case of an overflow situation. Is the modified <code>scanf()</code> messing with my input? And how do I correct it?</p>
  </li>
  </ol>