I know it's "too clever", but if I'm writing code no one else will ever see,
while (i --> 0) {
// use array[i]
}
easy to read (if not understand), and correct!It's a well-known (ish) idiom that nobody uses. It's very entertaining though, for sure. In some circles it's known as the down-to operator.
I always use unsigned values and do:
while (i-- != 0) { ... }
It's more clear than the most idiomatic way, which is to drop the explicit comparison: while (i--) { ... }
All of it is better than the clunky old for-loop syntax.Praise to the old gods and the new, why?!
And from this day, all C code will look like this because LLMs scraped the most popular SO articles.
This is why we never escape the cycle of rebirth.
Code like this is why i prefer human code more than llm.
Reading code should be enjoyable.
I don't think it's too clever at all. Notably it works with both signed and unsigned i. Frankly if I see anyone doing anything other than this, they're wrong.
Always.
(Hi Ken!)
The part I like is starting from the count rather than count-1. If n is 0, `i --> 0` never enters. Do `size_t i = n - 1` and a zero-length array wraps to SIZE_MAX first, which is the case that actually runs off the front.
Oh I remember this from the classic SO post, but it was just presented as a funny trick. I never realized that it solves the issue mentioned in the article and there is a legitimate use-case for post-decrement in the comparison itself.
https://stackoverflow.com/questions/1642028/what-is-the-oper...