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};
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Ren
  • 2,852
  • 2
  • 23
  • 45
  • I think you can even write `char buf[5] = ""`; that is also equivalent – Grijesh Chauhan May 04 '15 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](http://stackoverflow.com/a/30032426/539810) 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};`) –  May 05 '15 at 05:47
  • 1
    `char buf[5] = {0}` equivalent to `char buf[5]={0,}` and equivalent to `char buf[5]={0,0,0,0,0};` – ashish Jun 19 '15 at 13:55

4 Answers4

26

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 3
    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 May 04 '15 at 14:30
  • 3
    @haccks you answer a few questions like this, pretty soon [N1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) is the first thing in your browser drop-down when you hit "n"... – Alex Celeste May 05 '15 at 01:28
  • @haccks sir, I just _"quoted"_ the quote. You know what I mean. :-) – Sourav Ghosh May 05 '15 at 04:29
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

Gopi
  • 19,784
  • 4
  • 24
  • 36
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.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 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 May 04 '15 at 15:18
  • @Thomas; Its allowed in C99. Not sure about C89. I don have C89 standard to check it. – haccks May 04 '15 at 15:20
  • @Thomas; This feature is included in C99. Thanks @jxh for the link. – haccks May 04 '15 at 16:17
2

Yes, the result is the same for both.

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

Community
  • 1
  • 1
bpinhosilva
  • 692
  • 1
  • 5
  • 15