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");
}