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
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...
#1: Initial revision
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!