#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
This evaluates to an unsigned type. In your loop, however, d is a signed value. In an expression where a signed and unsigned value participate, the signed value is converted to an unsigned. But d is -1, which cannot fit in an unsigned, so it "wraps around" to the highest unsigned value on the machine (on 2's complement).