Created array not empty in c? [duplicate]

  • A+
Category:Languages

This question already has an answer here:

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!

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: