Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A Why is a for loop getting stuck when using a uint64_t counter, whereas a while loop isn't?

I asked this question a while ago over SE. When I use a for loop with a uint64_t as a counter, it gets stuck forever, even though the condition seems to be well defined. Offending MCVE #includ...

3 answers  ·  posted 6d ago by aura-lsprog-86‭  ·  last activity 4d ago by Lundin‭

Question c integer loops
#1: Initial revision by user avatar aura-lsprog-86‭ · 2025-02-15T07:46:07Z (6 days ago)
Why is a for loop getting stuck when using a uint64_t counter, whereas a while loop isn't?
_I asked this question a while ago over [SE](https://stackoverflow.com/q/35570379/5397930)._

<hr />

When I use a `for` loop with a `uint64_t` as a counter, it gets stuck forever, even though the condition seems to be well defined.

## Offending MCVE

    #include <stdio.h>
    #include <inttypes.h>

    int main() {
        uint64_t i;
        for (i = 15; i >= 0; i--) { printf("%"PRIu64" ", i); }
        return 0;
    }

### Partial output

    15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 18446744073709551615 18446744073709551614 18446744073709551613 18446744073709551612 18446744073709551611 18446744073709551610 18446744073709551609 18446744073709551608 18446744073709551607 18446744073709551606 18446744073709551605 18446744073709551604 18446744073709551603 18446744073709551602 18446744073709551601 18446744073709551600 18446744073709551599 18446744073709551598 18446744073709551597 18446744073709551596 18446744073709551595 18446744073709551594 18446744073709551593 18446744073709551592 18446744073709551591 18446744073709551590 18446744073709551589 18446744073709551588 18446744073709551587 18446744073709551586 18446744073709551585 18446744073709551584 18446744073709551583 18446744073709551582 18446744073709551581 18446744073709551580 18446744073709551579 18446744073709551578 18446744073709551577 18446744073709551576 18446744073709551575 18446744073709551574 18446744073709551573 18446744073709551572 18446744073709551571 18446744073709551570

It seems it's ignoring the stop condition, and so it rolls over.

However, when changing it to an "equivalent" while loop, everything works fine:

## Correct MCVE

    #include <stdio.h>
    #include <inttypes.h>

    int main() {
        uint64_t i = 16;
        while (i--) { printf("%"PRIu64" ", i); }
        return 0;
    }

### Complete output

    15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 

Am I missing something regarding the use of `uint64_t` counters in a `for` loop? Any help is greatly appreciated!