# The One-Line Trick for Iterating Backwards in C That Almost Everyone Gets Wrong

> Hacker News story 50249598 · language: en · submitted 2026-10-26
> Web version: https://zeli.app/story/50249598
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 107 · by [cremno](<https://news.ycombinator.com/user?id=cremno>) · 💬 75
[original](<https://www.davidpriver.com/Iterating-backwards-in-C.html>) · [HN discussion](<https://news.ycombinator.com/item?id=50249598>)

## Summary

A size_t index counting down to zero looks obvious but breaks: i >= 0 is always true, and an empty buffer wraps i to SIZE_MAX. Switching to ssize_t works but drags in signed-conversion traps, a POSIX-only type, and promotion issues on 32-bit. The clean fix is `for(size_t i = count; i--; )`, relying on post-decrement and well-defined unsigned wraparound to keep i in bounds.

## Key quote

> So much simpler! Let's break it down: The key is the postdecrement operator on i.

## HN discussion (75 comments)

### [kenferry](<https://news.ycombinator.com/item?id=50298795>)

> 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!

### [Joker_vD](<https://news.ycombinator.com/item?id=50299494>)

> That's the price of innovation, I guess; before C invented its "for"-loop, other languages had their equivalents generally looking like this:
>
> FOR var = start [DOWN] TO end BY step DO ... END
>
> No custom increment expressions, and "step" even usually was only allowed to be a constant (in which case, the language usually didn't have DOWN specifier and used the step's sign instead). Then Dennis Ritchie invented C, and ditched the traditional "do"-loop (Fortran started that construct with DO, so it was called DO loop; Algol used FOR instead, but most people preferred the Fortran's nomenclature... until C came to dominate, but that wouldn't happen for yet another 20 years) in favour of his invention because he felt that easily iterating through linked lists or strings was a much more pressing concern:
>
> for(p=head; p; p=p->next) { ... }
>
> for(s=str; *s; s++) { ... }
>
> The downside, of course, is that now you can't straightforwardly write
>
> for size_t i = count - 1 downto 0 do
>
> ...
>
> end

### [bugake](<https://news.ycombinator.com/item?id=50299267>)

> For the uninformed: counting backwards in a for loop is faster because comparing an unsigned integer against 0 is generally faster than comparing it against any other value.
>
> It also says not to use `for (i >= 0)` because any unsigned integer is >= 0, and the loop will never end.

### [kccqzy](<https://news.ycombinator.com/item?id=50299558>)

> This is why I prefer to use C++ and its reversed iterators. The rbegin and rend accessors are there in the containers from the beginning. It’s less error prone than the C version using indices. (Of course some situations require me to use indices, so I do use the i-->0 trick, but it is much rarer.)

### [randomblock1](<https://news.ycombinator.com/item?id=50299450>)

> 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.

---

## Cite

- Page: https://zeli.app/story/50249598
- Date: 2026-10-26
- Source: davidpriver.com (https://www.davidpriver.com/Iterating-backwards-in-C.html)
- Hacker News: https://news.ycombinator.com/item?id=50249598

## Other languages

- zh: https://zeli.app/zh/story/50249598 · markdown: https://zeli.app/zh/story/50249598.md
- ja: https://zeli.app/ja/story/50249598 · markdown: https://zeli.app/ja/story/50249598.md
- ko: https://zeli.app/ko/story/50249598 · markdown: https://zeli.app/ko/story/50249598.md
- de: https://zeli.app/de/story/50249598 · markdown: https://zeli.app/de/story/50249598.md
- es: https://zeli.app/es/story/50249598 · markdown: https://zeli.app/es/story/50249598.md
- ru: https://zeli.app/ru/story/50249598 · markdown: https://zeli.app/ru/story/50249598.md
