Because TOTAL_ELEMENTS
is an unsigned value (type size_t
) and d
is a signed value (type int
, which is most likely signed on your platform, and you're certainly assuming it is even if it isn't). The compiler in this case is converting d
to an unsigned value, and converting -1 to an unsigned value usually results in SIZE_MAX
or something similar, which is certainly greater than TOTAL_ELEMENTS - 2
. To do this correctly, cast the unsigned value to a signed value: (int)(TOTAL_ELEMENTS - 2)
.
Out of curiosity, why are you starting your index at -1 and then adding 1 to it in the loop? Why not just do this:
unsigned i;
for(i = 0; i < (TOTAL_ELEMENTS); i++)
printf("%d\n", array[i]);
It would be much clearer than what you have.