0

I was trying to do little bit of experimentation on array initialisation. In the program mentioned below ,I had initialised an array along with mentioning it's dimension and at the same time , I had mentioned the values of the array but the total number values of the array I had mentioned during initialisation is less than the dimension of the array , hence I was expecting some form of large garbage values (as shown in the 2nd program , which basically is to show that unless a value is assigned to an array's element will basically generate garbage values) , but the last value of this array is only producing 0 as an output. Why so ??

Program 1 -
(Where instead of large garbage value of the last element of the array the output which is being generated is 0.)

#include <stdio.h>
int main(void)
{
    int array[5]={1,2,3,4};
    printf("\nValues of array are :");
    for (int i = 0; i < 5; ++i)
    {
        printf("\t%d",array[i]);
    }
    printf("\n");
}

Program 2-
(Which basically is to show that values are assigned to an array , it just generates some large garbage value)

#include <stdio.h>
int main(void)
{
    //For accepting the size of an array
    int num;
    printf("\nEnter the size of an array:\t");
    scanf("%d",&num);
            //Declaration of an array
    int array[num];
    //Printing output of an array
    for (int i = 0; i < num; ++i)
    {
        printf("\nValue of array[%d]=%d",i,array[i]);
    }
    printf("\n-------------------------------------------------------\n");
    printf("Apparently you will be finding out garbage values as elements of array\n");
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Why would you expect "large" garbage values? Shouldn't garbage values be arbitrarily sized? – ad absurdum Oct 27 '17 at 13:30
  • @DavidBowling What I meant was that I wasn't expecting a single digit output like 0 - I was expecting an arbitrary size garbage value of size which could be represented as INT – Aashish Loknath Panigrahi Oct 27 '17 at 13:32
  • Even if `array[4]` was uninitialized in the 1st case (which it's not), `0` is a perfectly valid garbage value. – Kevin Oct 27 '17 at 13:33
  • @Kevin Yeah but unlike program 2 , garbage value varies everytime when the program is executed unlike program 1 where the output is only 0 for the last position – Aashish Loknath Panigrahi Oct 27 '17 at 13:36
  • 1
    @AashishLoknathPanigrahi -- really, there is no concept of "garbage values" in C. Instead, there are _indeterminate_ values. It is entirely possible for indeterminate values to remain the same for multiple executions of the program, or for them to be 0. – ad absurdum Oct 27 '17 at 13:41
  • @AashishLoknathPanigrahi 'Garbage' doesn't mean 'random', it just means 'whatever happens to be there' (in many cases). – Kevin Oct 27 '17 at 13:43

1 Answers1

5

For the first snippet. quoting C11, chapter §6.7.9

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 regarding the values to be used for initialization in case of objects with static storage duration, paragraph 10,

If an object that has static or thread storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

if it has arithmetic type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

So, in case, there are fewer initializers in the brace enclosed list, the remaining items would be set to 0 (as the type is int).

Whereas, for the second case, the array remains unitialized, so the contents are indeterminate.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Can you send me link of c11 documentation ?? – Aashish Loknath Panigrahi Oct 27 '17 at 13:38
  • @AashishLoknathPanigrahi the actual version is not free, however, a google search will provide you with draft versions like [this](https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwj1wOHI9pDXAhVj_IMKHVgwBsoQFggnMAA&url=http%3A%2F%2Fwww.open-std.org%2Fjtc1%2Fsc22%2Fwg14%2Fwww%2Fdocs%2Fn1256.pdf&usg=AOvVaw0okqHJHXipUk87OTYyEAS9) – Sourav Ghosh Oct 27 '17 at 13:39
  • @AashishLoknathPanigrahi -- [here is a link to the C11 Draft Standard, §6.7.9](http://port70.net/~nsz/c/c11/n1570.html#6.7.9). – ad absurdum Oct 27 '17 at 13:43