...
This is the preferred approach because the size of the array can always be derived even if the size of the string literal changes.
Exceptions
STR36-EX1EX0: If the intention is to create a character array and not a null-terminated byte string, initializing to fit exactly without a null byte is allowed but not recommended. The preferred approach to create an array containing just the three characters, 'a'
, 'b'
, and 'c'
, for example, is to declare each character literal as a separate element as follows:
...
Also, you should make clear in comments or documentation if a character array is, in fact, not a null-terminated byte string.
STR36-EX2EX1: If the char array must be larger than the string literal it is initialized with, you may explicitly specify an array bounds. This is particularly important if the array's contents might change during program execution.
...