Can we find the number of elements in an integer array when the capacity is greater the number of elements?
If by "find" you mean "have the compiler or run-time system to keep track of it for us, so we can ask", the answer is an emphatic no. With the exception of character arrays containing strings (which are always terminated by a \0
character, which strlen
or the equivalent can definitively find), there is no mechanism in C, not even a language-defined convention, for keeping track of the "used" size of an array. You must keep track of it yourself somehow. The usual way is with an auxiliary "count" value held in a separate variable. It is also possible to use a sentinel value, such as 0 or -1 or -999, if you know that those values won't occur in normal data.
I am aware that if I declare an array with a capacity of 10 and initialize only 5 values in it, then the remaining 5 elements will be uninitialized and their values will be unpredictable, which means they may contain garbage values.
That's correct if the array is allocated locally to a function (formally, if it has automatic duration). That's true for the array arr
in your example, although it would not be true for a global array (declared outside of any function), or one declared with the static
qualifier. (Those other kinds of arrays — formally, the ones with static duration — are guaranteed to be initialized to 0.)
The output is 0 for all the elements which means by default the uninitialized elements are initialized to 0 rather than having garbage values.
No, it just means the initial, "garbage" values happened to be 0 — this time.
Think about it this way. Suppose you go to the store and buy a brand-new garbage can. But it's completely clean! There's no garbage in it at all! It's so clean you could eat out of it! Was this false advertising? Did the store fraudulently sell you a non-garbage can?
Why is the local array initialized to 0 by default?
It is not initialized to 0 by default.
When will an uninitialized array have garbage values?
There are several answers:
- "Sometimes."
- Whenever it's least convenient for you.
- When the stack space has been previously used.
To say a little more about numbers 1 and 2:
I like to call this the
"confounding expectations" rule of uninitialized local variables in C.
Uninitialized local variables never start out holding what you expect.
If you expected them to be random, you'll find that
(at least on any given day) they're utterly repeatable and predictable, and often 0.
But if you expect them to be predictable (and, god help you,
if you write code that depends on it), then by jingo,
you'll find that they're quite random.
To say a little more about number 3:
There are no hard-and-fast rules here, but in general, local variables are allocated on the stack, and although the stack usually starts out containing all 0's, once you've had a few functions called and returned, they'll have left the last values of their local variables scattered all over the memory formerly occupied by their stack frames, and at that point, newly-called functions will find that their uninitialized local variables do not start out containing 0, but do start out containing "garbage".
In other words, the very first time a function gets called, and if it's got a brand-new region of the stack for its stack frame (that is, if no previous function has called deeper into the stack), it's like buying that brand-new, so-clean-you-could-eat-out-of-it garbage can, and uninitialized local variables in that function are likely to be 0, as they were in your example. But if it's not the first call, if its stack frame gets placed in a region of stack memory that has been used before, then it's likely to be garbage galore.