title: Buffer and its interaction with function?
body: <p>I am facing some problems while understanding the following code.
  It is a program to read Strings from keyboard if the length of the String is lesser than the specified size (i.e 'n' here).</p>
  
  <p>If the length of a string is larger than the specified size, the remaining characters on the line will be discarded.</p>
  
  <p>More specifically, I want to know what is happening inside the buffer and how <code>getchar()</code> is reading the data and not storing it in the buffer.  </p>
  
  <pre><code>char * s_gets(char * st, int n)
  {
       char * ret_val;
       int i = 0;
       ret_val = fgets(st, n, stdin);
  
       if (ret_val) // i.e., ret_val != NULL
       {
            while (st[i] != '\n' &amp;&amp; st[i] != '\0')
            i++;
  
            if (st[i] == '\n')
                 st[i] = '\0';
  
            else // must have words[i] == '\0'
            while (getchar() != '\n')
                  continue;
  
       }
  
       return ret_val;
  }
  </code></pre>