-1

I just created an array with size 10

int array[10];

Afterwards i wanted to check the values

for(int i = 0; i < 10; i++){
printf("%d\n", array[i]);
}

But the output was different from my expectation. I though that all the values should be 0, but the output says the following:

1996260900
1
0
1995115336
78836
78796
0
68600
0
0

Why is that?
Thanks in advance!

itzFlubby
  • 2,269
  • 1
  • 9
  • 31
  • btw. I didn't modify the array. The printing is right below the construction of it. – itzFlubby Jul 13 '18 at 11:58
  • 3
    The C standard does not *guarantee* that the initial value of an uninitialized variable us `0`. In fact, you're lucky to encounter this as early as now in your learning stage. Some compilers *do* automatically initialize variables to `0`, especially in debug mode – and thus inadvertently hide possible errors. – Jongware Jul 13 '18 at 14:21
  • you got garbage values because you only declared array and it doesn't initialized you can use loop for scan values from user like this for(int i=0;i<10;i++){ scanf("%d",&array[i]) } check this https://www.programiz.com/c-programming/c-for-loop – Dulanga Heshan Jul 15 '18 at 14:52

3 Answers3

10

The contents of an uninitialized local and non-static variable (array or not) is indeterminate and will seem almost random. Don't attempt to use such values.

If you want to initialize the values then you need to do it explicitly, as in

int array[10] = { 0 };  // Will initialize all elements to zero

Then a little nitpicking about your usage of the term "empty" together with arrays: Arrays are never "empty". If you define an array of ten elements, then it will always have ten elements.

And remember that C doesn't have any kind of "null value" concept. Everything has a value, which might be initialized or set by you, or it is uninitialized in which case the value is indeterminate.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Empty initializers are non-conforming, though some compilers may accept them. The conventional approach would be to use `{0}`. – John Bollinger Jul 13 '18 at 13:14
8

From C Standards#6.7.9p10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

To initialize all the elements of array with 0, you can do:

int array[10] = {0};

The int type object that has static or thread storage duration and not initialized explicitly, is initialized to zero. So, you can expect the value of all the elements of array array to be 0 only in case if it has static storage duration otherwise the value of elements of array array will be indeterminate and you have to initialize them explicitly with 0.

H.S.
  • 11,654
  • 2
  • 15
  • 32
0

When you declared the array, you didn't initialized it. So, it got initialized by garbage value. And, you are printing garbage value.

" This happens only in case of local varibales. As memory for local variables are allocated on stack and while allocating the memory the runtime system does not clear the memory before allocating it to the variable unlike in case of allocating memory in heap for global and static variables. Hence the default value of local varibles becomes the content of its memory on stack while that of constant and static variables is 0. "

Quoted from here

Abhishek Jaiswal
  • 243
  • 1
  • 18
  • 1
    The array didn't get "initialized by garbage values," it was never initialized at all, and so it contains _indeterminate_ values (in the language of the Standard). Also, stacks and heaps are not C language level details, but implementation level details. – ad absurdum Jul 13 '18 at 12:11