In the code below, I insert 6 elements in the array. The array size however is 5 and the for loop runs 7 times. The code then inserts and print the 7 elements.
It seems it doesn't matter what the size of the array is since I can apparently insert and print more elements. If that's the case, then why even declare a size for the array?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int index, block[5], number;
printf("\nEnter number of elements which you want to append :");
scanf("%d", &number);
printf("\nEnter the values :");
for (index = 0; index <= number; index++)
{
scanf("%d", &block[index]);
}
for (index = 0; index <= number; index++)
{
printf("%d\t", block[index]);
}
return (0);
}
Output:
Enter the values :1
2
3
4
5
6
7
1 2 3 4 5 6 7