title: Null terminator at the end of char array in C++
body: <p>Why it isn't necessary to store null character at the end of the string named temp in the following code</p>
  
  <pre><code>char source[50] = "hello world";
  char temp[50] = "anything";
  int i = 0;
  int j = 0;
  
  while (source[i] != '\0')
  {
      temp[j] = source[i];
      i = i + 1;
      j = j + 1;
  }
   cout &lt;&lt; temp; // hello world
  </code></pre>
  
  <p>while in the case below it becomes necessary</p>
  
  <pre><code>char source[50] = "hello world";
  char temp[50];
  int i = 0;
  int j = 0;
  while (source[i] != '\0')
  {
      temp[j] = source[i];
      i = i + 1;
      j = j + 1;
  }
  cout &lt;&lt; temp; // will give garbage after hello world
                // in order to correct this we need to put temp[j] = '\0' after the loop
  </code></pre>