<p>Here is a sample from Kernighan &amp; Ritchie's "The C Programming Language":</p>

<pre><code>int getline(char s[], int lim)
{
   int c, i = 0;

   while (--lim &gt; 0; &amp;&amp; (c=getchar()) !=EOF &amp;&amp; c !='\n')
   {
      s[i++] = c;
   }

   if (c =='\n') 
   {
      s[i++] = c;
   }

   s[i] = '\0';

   return i;
}
</code></pre>

<p>Why do we should check if <code>c != '\n'</code>, despite we use <code>s[i++] = c</code> after that?</p>

[{'answer_id': 71935, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>You do that just to exit the while loop on new line. Else you would have to check it in while body and use break.</p>\n'}, {'answer_id': 71952, 'author': 'Nick Johnson', 'author_id': 12030, 'author_profile': 'https://Stackoverflow.com/users/12030', 'pm_score': 1, 'selected': False, 'text': "<p>The comparison is to ensure readline terminates when it encounters a newline character (the '\\n'). On the iteration where it does, it terminates without adding the newline to the string, so the statement after that ensures that the string is always newline terminated, even if one of the other termination conditions was reached.</p>\n"}, {'answer_id': 71954, 'author': 'HitScan', 'author_id': 9490, 'author_profile': 'https://Stackoverflow.com/users/9490', 'pm_score': 0, 'selected': False, 'text': "<p>That ensures that you stop at the end of the line even if it's not the end of the input. Then if there is a newline the \\n is added to the end of the line and i incremented one more time to avoid overwriting it with the \\0.</p>\n"}, {'answer_id': 71988, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 0, 'selected': False, 'text': "<pre><code>int getline(char s[], int lim)\n{\n   int c, i;\n   i=0;\n   /* While staying withing limit and there is a char in stdin and it's not new line sign */\n   while (--lim &gt; 0; &amp;&amp; (c=getchar()) !=EOF &amp;&amp; c !='\\n')\n   /* Store char at the current position in array, advance current pos by one */\n      s[i++] = c;\n   /* If While loop stopped on new-line, store it in array, advance current pos by one */\n   if (c =='\\n') \n      s[i++] = c;\n   /* finally terminate string with \\0 */\n   s[i] = '\\0';\n   return i;\n}\n</code></pre>\n"}, {'answer_id': 71999, 'author': 'aggergren', 'author_id': 7742, 'author_profile': 'https://Stackoverflow.com/users/7742', 'pm_score': 3, 'selected': True, 'text': "<p>The functions reads characters from the standard input until either EOF or a newline characters is found. </p>\n\n<p>The second check ensures that the only newline character is put into the char array. EOF shouldn't occur in a proper c-string. Also, if the character isn't newline that means that we might have filled up our c-string, in which case we shouldn't put any more characters into it. </p>\n\n<p>Notice we still append the '\\0'. We've ensured that theres still room for one more character in our c-string, as we use the pre-fix decrementor, which evaluates before the comparison.</p>\n"}, {'answer_id': 72017, 'author': 'auramo', 'author_id': 4110, 'author_profile': 'https://Stackoverflow.com/users/4110', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not sure whether I understand the question. <code>c !='\\n'</code> is used to stop reading the line when the end of line (linefeed) occurs. Otherwise we would always read it until the limit even if it ends before. The first <code>s[i++] = c;</code> in the while-loop doesn't occur if a linefeed has been reached. That's why there is the special test afterwards and the other <code>s[i++] = c;</code> in case it was a linefeed which broke the loop.</p>\n"}, {'answer_id': 72069, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Not answering your question, but I'll write some comments anyway:</p>\n\n<p>I don't remember all K&amp;R rules, but the function you've listed will fail if lim is equal to one. Then you won't run the loop which leaves c unintialised, but you'll still use the variable in the if (c == '\\n') check.</p>\n\n<p>Also the while (--lm > 0; ...) thing will not go through the compiler. Remove the ';' and it does.</p>\n"}, {'answer_id': 83291, 'author': 'user11211', 'author_id': 11211, 'author_profile': 'https://Stackoverflow.com/users/11211', 'pm_score': 1, 'selected': False, 'text': "<p>There is a bug in the code.</p>\n\n<p>If the size of s is N bytes and the user types a newline as the (N-1)th character, the Nth character will become a '\\n' and the (N+1)th character (which is not allocated) will become a '\\0'.</p>\n"}]
['https://Stackoverflow.com/questions/71913', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11972/']