---
title: "Iterating Backwards in C"
slug: iterating-backwards-in-c
url: https://listedarticles.com/articles/iterating-backwards-in-c
canonical_url: https://www.davidpriver.com/Iterating-backwards-in-C.html
content_type: tutorial
language: en
published_at: 2026-10-30T02:07:51.637Z
updated_at: 2026-10-30T02:07:51.637Z
author: "David Priver"
author_url: https://www.davidpriver.com/
authored_by: human
publisher: "David Priver"
publisher_url: https://www.davidpriver.com/
topics: ["programming", "c"]
license: all-rights-reserved
word_count: 444
reading_minutes: 2
citation: "David Priver, David Priver. \"Iterating Backwards in C.\" 30 Oct 2026. https://www.davidpriver.com/Iterating-backwards-in-C.html (all-rights-reserved)"
# The full text follows. The web page shows an extract and sends readers
# to the source above; quote the citation and link the canonical URL.
---

# Iterating Backwards in C

> David Priver shows the simple, correct way to loop backwards over a buffer with an unsigned size_t counter in C, avoiding signed-index conversions and off-by-one bugs he says he keeps seeing, even more so in LLM-written code.

# Iterating Backwards in C

This is shorter than some of my other posts, but I keep seeing people
doing this wrong. It's even worse now in the LLM era.

Let's say you have a buffer and a counter and you need to iterate it
backwards for some reason:

```
_Bool find_last(size_t count, int *haystack,
                int needle, size_t *where);
```

Almost everyone can do the forward version on autopilot:

```
for(size_t i = 0; i < count; i++){
  if(haystack[i] == needle){
    *where = i;
    return 1;
  }
}
return 0;
```

So they try to do the same thing, just iterating backwards:

```
for(size_t i = count - 1; i >= 0; i--){
  // find needle
}
```

But that doesn't work at all! i >=0 is tautologically true
(modern compilers will warn). Even worse, if count is 0, i immediately becomes
SIZE\_MAX.

Ok, so they try the next version:

```
for(ssize_t i = count - 1; i >= 0; i--){
  // find needle
}
```

This works in practice with a signed index but introduces
some problems:

- Now your index variable is signed, which can cause problems when used in expressions due to
  C's conversion rules.
- ssize\_t is a POSIX thing so you'd have to define it yourself on windows.
- If your type is 32 bit, you run into promotion problems and might even run into truncation problems.
- This spelling of the loop is overcomplicated and requires special inspection
  each time. Did you get the second-clause condition right?

Avoiding these problems brings us to the actual, proper way to iterate backwards in C:

```
for(size_t i = count; i--; ){
  // find needle
}
```

So much simpler! Let's break it down:
The key is the postdecrement operator on i.
It decrements i but yields the value that i was before the operation.
In this case, this means that in the body of the loop i is always in bounds
as its value will be decremented after initialization so it is at most count - 1,
while the condition checks that if it was previously 0 then the loop ends. i will
wrap around, but unsigned wrap-around is well defined in C so that is not a problem.

The final version:

```
_Bool find_last(size_t count, int *haystack,
                int needle, size_t *where){
  for(size_t i = count; i--; ){
    if(haystack[i] == needle){
      *where = i;
      return 1;
    }
  }
  return 0;
}
```

Doing it this way means you don't have to mess around with converting to a signed
loop index and look at how simple that loop is! You can tell at a glance
if someone has implemented the reverse loop properly or not.

So please! If you see someone trying to iterate backwards, show them this way
to do it!
