The problem is that (TOTAL_ELEMENTS-2)
is an unsigned value. When you make the comparison d <= (TOTAL_ELEMENTS-2)
, both values are converted to unsigned values, and the result is false.
In your second example, x
is signed so there is no problem.