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};
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
static
storage duration.
Yes when you initialize one element in the array to 0
the rest are set to 0
char buf[5] = {0};
char buf[5] = "";
Both are same
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.
Yes, the result is the same for both.
There are few related questions with more discussions here, and a possible duplicated question here.