Q:

Declared vs non-declared array in C

I came across these two blocks of code:
#include <stdio.h>
int main() {
  int a[10], i;
  for (i = 1; i <= 10; i++) {
    scanf("%d", &a[i]);
    printf("%d\n", a[i]);
  }
  return 0
}

When I run the first piece of code, the code runs well but it gets overridden at some point and I do not get the expected result.
However, when I run the second piece of code the program runs perfectly fine with no error.
#include <stdio.h>
int main() {
  int size;
  scanf("%d", &size);
  int a[size], i;
  for (i = 1; i <= size; i++) {
    scanf("%d", &a[i]);
    printf("%d\n", a[i]);
  }
  return 0
}

Why does the program run perfectly in the second case? Even in the second case, the subscript goes past the declared array size.

A:

Both code snippet has array out of bound access so you see undefined behavior
for(i=1;i<=size;i++)

If you have array of size 10 then the valid index is 0-9
Once we say undefined behavior then anything might happen even a crash. When you say it is working then it is just by luck you might not be seeing a crash but still you are accessing some memory which you have not allocated 

