17

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};
3
  • I think you can even write char buf[5] = ""; that is also equivalent Commented May 4, 2015 at 19:16
  • 1
    Yes, they're equivalent. Please be aware that char buf[5] = {4}; is equivalent to char 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 have static storage 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 to char buf[5] = {0,0,2,0,0};) Commented May 5, 2015 at 5:47
  • 1
    char buf[5] = {0} equivalent to char buf[5]={0,} and equivalent to char buf[5]={0,0,0,0,0}; Commented Jun 19, 2015 at 13:55

4 Answers 4

27

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.

Sign up to request clarification or add additional context in comments.

3 Comments

For a moment I was paused when you posted the answer with quote from standard (within few seconds). How fast you did that :O I am still surprised
@haccks you answer a few questions like this, pretty soon N1570 is the first thing in your browser drop-down when you hit "n"...
@haccks sir, I just "quoted" the quote. You know what I mean. :-)
6

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

Comments

4

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.

3 Comments

Is {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?
@Thomas; Its allowed in C99. Not sure about C89. I don have C89 standard to check it.
@Thomas; This feature is included in C99. Thanks @jxh for the link.
2

Yes, the result is the same for both.

There are few related questions with more discussions here, and a possible duplicated question here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.