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.

Comments on Why is a for loop getting stuck when using a uint64_t counter, whereas a while loop isn't?

Parent

Why is a for loop getting stuck when using a uint64_t counter, whereas a while loop isn't?

+1
−0

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

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+0
−0

SE user DU Jiaen provided the following answer, edited by Martin Zabel and found here.


The expression i >= 0 is always true if i is of an unsigned integer type. The alternative and simple way is to change it into:

uint64_t i;
for (i = 15; i != -1; i--) { ... }

This works no matter if i is a signed or an unsigned integer. In the uint64_t case, -1 will first get converted into 0xFFFFFFFFFFFFFFFF and then compared with i.

If you want to get rid of the compile warning, change it into:

i != (uint64_t)-1

but you need to ensure the uint64_t is exactly the type of i.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

i != -1 is brittle (1 comment)
i != -1 is brittle
Lundin‭ wrote 4 days ago

We shouldn't compare unsigned numbers against signed ones. That i != -1 check is brittle. Consider what will happen if we just change from 64 to 16 bit: for (uint16_t i = 15; i != -1; i--) { printf("%"PRIu16 " ",i); } Oops. Here i actually gets promoted to a signed type before the comparison, but since a 32 bit int can hold all values of a 16 bit unsigned, there are never any negative numbers.