lundi 12 février 2018

Why does my C code crash?

I'm trying out the syntax in C, as it's new to me. I was just starting with arrays, loops and random numbers, so I wrote this really simple test code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand(time(NULL));
    int arr[9];

    for(int i=0; i<10; i++) {
        arr[i] = rand();
        printf("%d \n", arr[i]);
    }
}

I compile it with gcc and run from command prompt. Runs fine, prints each element in the array... And then crashes. Why? Is there something with the code logic?

Thank you in advance! This is my first question here, so I apologize in advance if I made any mistakes in question asking rules.




Aucun commentaire:

Enregistrer un commentaire