5

Hello I am a beginner in C programming language, recently i started learning arrays, I have studied that by default all values in an int array are garbage.

Then why i am getting different values in these two cases.

Case-1

int arr[5];

in this case from arr[0] till arr[4] we will have garbage values, but in next case.

Case-2

int arr[5] = {1};

in this case arr[0] will have a value 1 and remaining from arr[1] to arr[4] will have value 0.

My question is that, When in case-1 each un-initilized array locations are having garbage valeus then why in case-2 remaining un-initilized array locations are having 0 as default value.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
Mayank Tiwari
  • 2,974
  • 5
  • 30
  • 52
  • 10
    Because the standard says that when you initialise any array element, all the remaining not explicitly initialised elements are initialised as if they had static storage duration. It's mandated by the standard. – Daniel Fischer Jul 08 '13 at 13:45
  • Out of curiousity: are you declaring this variable at global scope (i.e. as a static variable) or inside a function? If inside a function then your junk values are because the array is declared on the stack. – PP. Jul 08 '13 at 13:49
  • @PP. i am declaring these array variables inside a function.... – Mayank Tiwari Jul 08 '13 at 13:53
  • 1
    see also: http://stackoverflow.com/q/201101/1025391 – moooeeeep Jul 08 '13 at 13:57
  • 1
    You could get an answer to this "why" even in any C book(array chapter). – 0decimal0 Jul 08 '13 at 14:29

4 Answers4

12

C11 6.7.9 Initialization p19 covers this (my emphasis)

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

Section 6.7.9 p10 states that

If an object that has static or thread storage duration is not initialized explicitly, then...if it has arithmetic type, it is initialized to (positive or unsigned) zero;

simonc
  • 41,632
  • 12
  • 85
  • 103
  • even char `a[10] = {1};` initialize rest all with `0`, what is importance of *`arithmetic type`* ? – Grijesh Chauhan Jul 08 '13 at 14:12
  • Note that global variables have static storage duration (no need to declare them `static` in this case). – moooeeeep Jul 08 '13 at 14:13
  • @GrijeshChauhan I remove some bullet points from 6.7.9.10 that weren't relevant here. These concern members of pointer, aggregate and union types. I could possibly have removed mention of arithmetic types too but it seemed acceptable to retain such a small bullet point unchanged – simonc Jul 08 '13 at 14:14
10

The C99 draft says:

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.

And static objects are initialized to zero.

So, there's a large difference between not having any initializer at all, that gives you the uninitialized contents of memory (what you call "garbage"), and having an initializer. If the initializer is there, but missing data, you get 0 by default.

This is very handy, since it makes it possible to 0-initialize a large array by doing just as you did.

unwind
  • 391,730
  • 64
  • 469
  • 606
4

The bit you are missing is that if you initialize just one element of an array, the rest of its elements will be automatically initialized to 0. The language is defined in this way.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
1

from the C standard as cited here (hit the link, you can find some more useful info there)

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.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Stefano Falasca
  • 8,837
  • 2
  • 18
  • 24