title: Why/when to include terminating '\0' character for C Strings?
body: <p>I'm very new to C and am a bit confused as to when we need to manually add the terminating '\0' character to strings. Given this function to calculate string length (for clarity's sake):</p>
  
  <pre><code>int stringLength(char string[])
  {
      int i = 0;
      while (string[i] != '\0') {
          i++;
  }
      return i;
  }
  </code></pre>
  
  <p>which calculates the string's length based on the null terminating character. So, using the following cases, what is the role of the '\0' character, if any?</p>
  
  <p>Case 1:  </p>
  
  <pre><code>char * stack1 = "stack"; 
  printf("WORD %s\n", stack1);
  printf("Length %d\n", stringLength(stack1));
  </code></pre>
  
  <p>Prints:</p>
  
  <pre><code>WORD stack
  Length 5
  </code></pre>
  
  <p>Case 2: </p>
  
  <pre><code>char stack2[5] = "stack";
  printf("WORD %s\n", stack2);
  printf("Length %d\n", stringLength(stack2));
  </code></pre>
  
  <p>Prints:</p>
  
  <pre><code>WORD stack���
  Length 8
  </code></pre>
  
  <p>(These results vary each time, but are never correct). </p>
  
  <p>Case 3: </p>
  
  <pre><code>char stack3[6] = "stack";
  printf("WORD %s\n", stack3);
  printf("Length %d\n", stringLength(stack3));
  </code></pre>
  
  <p>Prints:</p>
  
  <pre><code>WORD stack
  Length 5
  </code></pre>
  
  <p>Case 4: </p>
  
  <pre><code>char stack4[6] = "stack";
  stack4[5] = '\0';
  printf("WORD %s\n", stack4);
  printf("Length %d\n", stringLength(stack4));
  </code></pre>
  
  <p>Prints:</p>
  
  <pre><code>WORD stack
  Length 5
  </code></pre>
  
  <p>Case 5:</p>
  
  <pre><code>char * stack5 = malloc(sizeof(char) * 5);
  if (stack5 != NULL) {
      stack5[0] = 's';
      stack5[1] = 't';
      stack5[2] = 'a';
      stack5[3] = 'c';
      stack5[4] = 'k';
      printf("WORD %s\n", stack5);
      printf("Length %d\n", stringLength(stack5));
  }
  free(stack5);
  </code></pre>
  
  <p>Prints:</p>
  
  <pre><code>WORD stack
  Length 5
  </code></pre>
  
  <p>Case 6: </p>
  
  <pre><code>char * stack6 = malloc(sizeof(char) * 6);
  if (stack6 != NULL) {
      stack6[0] = 's';
      stack6[1] = 't';
      stack6[2] = 'a';
      stack6[3] = 'c';
      stack6[4] = 'k';
      stack6[5] = '\0';
      printf("WORD %s\n", stack6);
      printf("Length %d\n", stringLength(stack6));
  }
  free(stack6);
  </code></pre>
  
  <p>Prints:</p>
  
  <pre><code>WORD stack
  Length 5
  </code></pre>
  
  <p>Namely, I would like to know the difference between cases 1, 2, 3, and 4 (also why the erratic behavior of case 2 and no need to specify the null-terminating character in 1 and 3. Also, how 3 and 4 both work the same?) and how 5 and 6 print out the same thing even though not enough memory is allocated in case 5 for the null-terminating character (since only 5 char slots are allocated for each letter in "slack", how does it detect a '\0' character, i.e. the 6th character?)</p>
  
  <p>I'm so sorry for this absurdly long question, it's just I couldn't find a good didactic explanation on these specific instances anywhere else</p>