#include <stdio.h>
int main()
{
int arr[5], size, i, pos, elem;
printf("Enter the size of array(less than 100)\n");
scanf("%d",&size);
printf("\nEnter the elements of array one by one\n");
for ( i = 0; i < size; i++)
scanf("%d",&arr[i]);
printf("\nEnter the position of insertion\n");
scanf("%d",&pos);
printf("\nEnter the element\n");
scanf("%d",&elem);
for ( i = size - 1 ; i >= pos - 1; i--)
arr[i + 1] = arr[i];
arr[pos - 1] = elem;
printf("\nInserted array is\n");
for ( i = 0; i <= size; i++)
printf("\t%d",arr[i]);
printf("\n");
return 0;
}
Since the array is int arr[5] it shouldn't be possible to insert an element at any index not in range [0,1,2,3,4]. Why am I able to insert at arr[6] and get correct answer still?.