# a[14]={0}?

• 04-05-2006
sybariticak47
a[14]={0}?
Hi! I've seen in some books notation like :
Code:

```a[14]={0}; char a[28]={'#'}```
What does this do? Thanks!
• 04-05-2006
joeprogrammer
That does not work because you are redifining "a"; try it; you will end up with a compiler error. I have no idea why they would do that.

Unless those are 2 different lines of code (ie, in a different program). In that case, it probably fills the first element of the array with the charecter.
• 04-05-2006
sybariticak47
yeah they are two separate codes. So it only fill the first element?
• 04-05-2006
laserlight
Quote:

So it only fill the first element?
Not quite. In the case of:
Code:

`int a[14] = {0};`
the array is zero initialised, i.e. all 14 elements will be set to 0.
For the string:
Code:

`char b[28] = {'#'};`
the first character is set to '#', the rest are left as null characters.
• 04-05-2006
sybariticak47
Thanks for the help
• 04-05-2006
7stud
Hi,

Just by way of further explanation: if you use an initializer list when you declare an array, i.e.:
Code:

`int nums[3] = {1, 2};`
and your initializer list does not contain the same number of elements as the size of the array, the remaining elements in the array will be automatically initialized to 0. 0 will be converted to the appropriate type for the array, e.g. a 0 for a char array is the '\0' character. So you can initialize a whole array to 0 doing this:

int char[20] = {0};

The first element is initialized to 0 or a '\0' character, and since the initializer list doesn't contain values for the rest of the array, the remaining elements are automatically initialized to '\0'. Now, if you do this:

char a[28]={'#'};

That assigns '#' to the first element in the array, and since the initializer list doesn't contain values for the rest of the array, the remaining elements are automatically initialized to '\0'.