Why I input 21 elements and it works fine? and If I input 22 it appears segmentation fault/stack smashing.(OS Linux). If I allocated only 20 elements. I've tried some sort of online compilers and there I could even write more elements.
My logic is that if we have 20 elements then we have elements of array from index 0 till index 19, and 20 index was written by machine with '\0' automatically.
Could you please explain to me how it works. Why It allows to write where you are not allocated to. And why stack smashing/segmentation fault appears on 22 elements rather than 21 elements.
Or could you please let me know what books do I have to use to find information about.
There is a piece of code below.
int n, i;
int a[20];
printf("Enter no of emelents of array\n");
scanf("%d", &n);
printf("\nEnter %d elements: ", n);
for(i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}