I am not sure what will be in the char array after initialization in the following way:
char buf[5]={0,};
Is that equivalent to
char buf[5]={0,0,0,0,0};
Yes, it is the same. If there are less number of initializers than the elements in the array, then the remaining elements will be initialized as if the objects having static storage duration, (i.e., with 0).
So,
char buf[5]={0,};
is equivalent to
char buf[5]={0,0,0,0,0};
Related Reading : From the C11 standard document, chapter 6.7.9, initalization,
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have
staticstorage duration.
Yes.
char buf[5]={0,}; // Rest will be initialized to 0 by default
is equivalent to
char buf[5]={0,0,0,0,0};
If the initializer is shorter than the array length, then the remaining elements of that array are given the value 0 implicitly.
You should also note that {0,} (trailing commas makes array easier to modify) is equivalent to {0} as an initializer list.
{0,} also equivalent to {0} in C89 where you generally can't have commas at the end of a comma-delimited list? Or is it strictly C99 only?
char buf[5] = ""; that is also equivalentchar buf[5] = {4};is equivalent tochar buf[5] = {4,0,0,0,0};. The currently accepted answer alludes to this fact with the quote from the C11 standard: "the remainder of the aggregate shall be initialized implicitly the same as objects that havestaticstorage duration." This also applies to structure types as they are also aggregate types. Unreferenced members/sub-objects w.r.t. designated initializers behave the same (char buf[5] = {[2] = 2};is equivalent tochar buf[5] = {0,0,2,0,0};)char buf[5] = {0}equivalent tochar buf[5]={0,}and equivalent tochar buf[5]={0,0,0,0,0};