PAVE: Initializing Character Arrays

Character arrays can be initialized in one of two ways:

  • Individually, as follows:

    char chABCD[4] = { 'a', 'b', 'c', 'd' };
  • With a string, as follows:

    char chABCD[5] = "abcd";

In the second case, where the character array is initialized with a string, the compiler appends a trailing '\0' (end-of-string character). Therefore, the array must be at least one larger than the number of characters in the string.

Because most string handling uses the standard library functions or relies on the presence of the trailing end-of-string character, it is common to see unbounded array declarations initialized with strings:

char chABCD[] = "ABCD";