Hey guys.
I tried experimenting with deliberately putting in more elements than an array has and printing out the results. The reason im doing this is basically in anticipation that should the user do something wrong, I want to understand how to handle these scenarios.
I write this code:
So if you input, say:Code:#include <stdio.h> int main(void) { int a[5], b, c; for (b=0; b< 10&&(scanf("%d", &c)); b++) a[b]=c; for(b=0;b<15;b++) printf("%d ", a[b]); return 0; }
It prints out:1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Q's:1, 2, 3, 4, 5, 10, 7, 8, 9, 10, a large number, another large number, etc until the fifteenth last element which, interestingly, contains a 1
1) I am printing out elements of an array that dont really exist (>5), and yet, it still prints out the same numbers I put in input. Is this because, somehow, all your inputs are "contiguous" next to the defined array?
2) If so, how come the last input (10) replaces 6 in the start of the numbers that go out of array bounds?
I guess its a silly non trivial attempt to understand the deeper workings of C