I am reading Carnegie Mellon slides on computer systems for my quiz. In the slide page 49 :
Counting Down with Unsigned
Proper way to use unsigned as loop indexunsigned i; for (i = cnt-2; i < cnt; i--) a[i] += a[i+1];
Even better
size_t i; for (i = cnt-2; i < cnt; i--) a[i] += a[i+1];
I don't get why it's not going to be infinite loop. I am decrementing i
and it is unsigned so it should be always less than cnt
. Please explain.
unsigned i = cnt - 1; while (i--) a[i] = a[i+1];
? – Synthiasyntonic