In C, you can partially initialize a structure or array, as a result of which elements / elements that are not mentioned in the initializer are initialized to zero. (Section C99 6.7.8.19). For example: -
int a[4] = {1, 2};
You can also initialize the "character type array" with a string literal (section C99 6.7.8.14), and "consecutive characters ... initialize the elements of the array". For example: -
char b[4] = "abc"; // b[0] == 'a' // b[1] == 'b' // b[2] == 'c' // b[3] == '\0'
Everything is pretty simple. But what happens if you explicitly specify the length of the array, but use a literal too short to fill the array? Are the remaining characters zero-initialized or undefined?
char c[4] = "a"; // c[0] == 'a' // c[1] == '\0' // c[2] == ? // c[3] == ?
Seeing it as a partial initializer would make sense, it would make char c[4] = "a" behave exactly like char c[4] = {'a'} , and it would have a useful side effect, allowing you with zero initialization of the array of the whole character with char d[N] = "" , but it is not at all clear to me that what the specification requires.
c string initialization literals
Dan Hulme Aug 02 2018-12-12T00: 00Z
source share