0

I've been using C for a while and this code surprised me.

int main(void) {
    int array[] = {100};
    int i;
    for (i = 0; i<100; i++){
        printf("%d \n",array[i]);
    }
return 0;
}

When I execute this program, it doesn't crash. In the for loop, where is the number 100 put any value that you want. Beyond the array[0] position, the program outputs random numbers. I hope that someone could help me with that.

joan2404
  • 315
  • 2
  • 4
  • 14
  • Is there any rule telling it *should* crash? – Eugene Sh. Nov 23 '16 at 20:26
  • 2
    There are many, many questions like this already here on SO, the answer is always: undefined behavior permits anything to happen, including the program not crashing. – alain Nov 23 '16 at 20:26
  • Because it didn't. A better question would be to ask why something *did* crash. – Weather Vane Nov 23 '16 at 20:27
  • It might not crash now, but it might crash later. And in 5 hours, it might not crash again. Later, when the sun burns out, it miraculously runs on its own, and it doesn't crash, except it won't stop running. In other words, [see _Undefined, unspecified and implementation-defined behavior_](http://stackoverflow.com/q/2397984/539810) –  Nov 23 '16 at 20:30

0 Answers0