title: C compiler Differences? NUL control character in Char
body: <p>Below is C code that will generate char array but with explicitly adding Null character or not. The results are unexpected in two compiler and I'm not sure why we even have to explicitly add the Null character?</p>
  
  <pre><code>//
  //  stringBugorNot.c
  //  
  //
  //
  
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  
   int main(void)
  {
       char aString[3] = {'a', 'b','c'};
       char bString[4] = {'a', 'b', 'c', '\0'};
  
       printf("\n");
       printf("len of a is: %lu\n", strlen(aString));
       printf("len of b is: %lu\n", strlen(bString));
  
       printf("\n");
  
       //Portion A
  
       printf("last element of a is: '%c'\n", aString[strlen(aString)]);
       printf("last element of b is: '%c'\n", bString[strlen(bString)]);
  
       printf("\n");
  
       //Portion B
  
       printf("last element of a is: '%c'\n", aString[strlen(aString) - 1]);
       printf("last element of b is: '%c'\n", bString[strlen(bString) - 1]);
  
  
  }
  </code></pre>
  
  <p><strong>Comments</strong></p>
  
  <p>+clang will give a runtime error because out of bounds on "aString".. makes sense
   +gcc will not give any error and simply output "nothing" the null as expected. But maybe gcc is smarter and adds the null for me? Is the actual memory size different??</p>
  
  <hr>
  
  <p><strong>Clang OUTPUT ----></strong></p>
  
  <p>len of a is: 3</p>
  
  <p>len of b is: 3</p>
  
  <p>bugOrNot.c:16:41: runtime error: index 3 out of bounds for type 'char [3]'</p>
  
  <p>last element of a is: ''</p>
  
  <p>last element of b is: ''</p>
  
  <p>last element of a is: 'c'</p>
  
  <p>last element of b is: 'c'</p>
  
  <hr>
  
  <p><strong>GCC OUTPUT ----></strong></p>
  
  <p>len of a is: 9</p>
  
  <p>len of b is: 3</p>
  
  <p>last element of a is: ''</p>
  
  <p>last element of b is: ''</p>
  
  <p>last element of a is: ''</p>
  
  <p>last element of b is: 'c'</p>