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:
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.