title: Why do we need to add a '\0' (null) at the end of a character array in C?
body: <p>Why do we need to add a '\0' (null) at the end of a character array in C?
  I've read it in K&amp;R 2 (1.9 Character Array). The code in the book to find the longest string is as follows :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #define MAXLINE 1000
  int readline(char line[], int maxline);
  void copy(char to[], char from[]);
  
  main() {
      int len;
      int max;
      char line[MAXLINE];
      char longest[MAXLINE];
      max = 0;
      while ((len = readline(line, MAXLINE)) &gt; 0)
          if (len &gt; max) {
              max = len;
              copy(longest, line);
          }
      if (max &gt; 0)
          printf("%s", longest);
      return 0;
  }
  
  int readline(char s[],int lim) {
      int c, i;
      for (i=0; i &lt; lim-1 &amp;&amp; (c=getchar())!=EOF &amp;&amp; c!='\n'; ++i)
          s[i] = c;
      if (c == '\n') {
          s[i] = c;
          ++i;
      }
      s[i] = '\0'; //WHY DO WE DO THIS???
      return i;
  }
  
  void copy(char to[], char from[]) {
      int i;
      i = 0;
      while ((to[i] = from[i]) != '\0')
          ++i;
  }
  </code></pre>
  
  <p>My Question is why do we set the last element of the character array as '\0'?
  The program works fine without it...
  Please help me...</p>