I output of below code is undefined when I ran multiple times. I would like to know why the output is undefined and what will be implication when I try to assign value to an array of unknown bound.
#include <stdio.h>
int main()
{
int a[]= {};
int num_of_elements;
int count = 0;
printf("Enter the number of elements:\n");
scanf("%d",&num_of_elements);
printf("Enter the numbers:\n");
for(count=0; count<num_of_elements; ++count)
{
scanf("%d", &a[count]);
}
printf("\n");
for(count=0; count<num_of_elements; count++)
{
printf("%d, ", a[count]);
}
printf("\n");
return 0;
}
Output when run at different times:
Enter the number of elements:
2
Enter the numbers:
1 2
0, 0,
Enter the number of elements:
3
Enter the numbers:
1 2 3
0, 0, 2,
Enter the number of elements:
4
Enter the numbers:
1 2 3 4
0, 0, 2, 3,
Segmentation fault
Enter the number of elements:
5
Enter the numbers:
1 2 3 4 5
0, 0, 2, 3, 4,
Segmentation fault