I wrote a C
codes :
#include <stdio.h>
int main()
{
int i;
int a[4];
for( i =0;i<5;i++)
{
a[i]=i;
printf("a[%d]:%d ",i,a[i]);
}
return 0;
}
I used gcc to compile it and succeed to run,the result is :
a[0]:0 a[1]:1 a[2]:2 a[3]:3 a[4]:4
But you can see array a
has size of 4,from 0 to 3,i haven't create extra memory to refer to a[4]
,and unexpectedly it run and seem no errors.In Java
,i find if arrays' index out of bounds,it'll throws exception.I just don't know why in C language it just run like right.