In C, the code snippet:
int i;
double x[10];
for (i = 0; i <= 10; i++) {
x[i] = (double) i;
printf("%f\n", x[i]);
}
produces the following output:
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
However, in Java the code snippet(same code) just syntactically different:
int i;
double[] x = new double[10];
for (i = 0; i <= 10; i++) {
x[i] = (double) i;
System.out.printf("%f\n", x[i]);
}
produces the following output:
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Array.main(Array.java:14)
Why does the C compiler not flag the attempt to access outside the array bounds? Am I missing something here?