logoalt Hacker News

randomblock1 • today at 3:10 AM • 2 replies • view on HN

I thought most people used the explicit version:

  for (size_t i = count; i > 0; --i) {
      // use array[i - 1]
  }
You can also do something like:

  size_t index = i - 1;
to not have to use i-1. I believe the compiler optimizes that out anyway to be identical.

Replies

throw-qqqqq • today at 7:51 AM

I agree that more explicit is almost always better in C. It’s difficult enough as is :D

I would do it exactly like you suggest, but using an explicitly sized type like uint32_t instead.

If I write firmware or low-level code, I almost exclusively use integers with explicit bitwidth.

seeknotfind • today at 3:28 AM

Yes, this is the best answer. This also works with `-fsanitize=unsigned-integer-overflow`.

➕ show 1 reply