News for hackers. Stuff that matters.
Re:Modern C [pdf] by renlo on Monday November 28, 2016 @04:08PM (#13058436)
Your for loop works, whereas the one in the parent comment would run forever, correct? Is there something I'm missing here?

edit: Ok, looking at it again the parent example is probably going to overflow or something right?

Re:Modern C [pdf] by cygx on Monday November 28, 2016 @04:26PM (#13058573)
Your for loop works, whereas the one in the parent comment would run forever, correct?

The other way around: size_t is an unsigned type, so decrementing 0 will wrap around to SIZE_MAX, a value that is positive as well as greater than 9. This means counter to your intuition, the first loop will terminate and the second one won't.

Re:Modern C [pdf] by imron on Tuesday November 29, 2016 @05:24AM (#13062147)
> Is there something I'm missing here?

Yes (see other replies), and it's precisely the reason why this code shouldn't pass code review.

At first glance the first example looks like it should fail but in fact works. The second example I provided looks like it should work, but in fact loops infinitely.

The first one is tricky and nifty, but prone to bugs. Using signed counters is better way to go about it.

Re:Modern C [pdf] by clarkcox3 on Monday November 28, 2016 @04:49PM (#13058733)
The example with ">= 0" is an infinite loop, a size_t will always be greater than or equal to zero; it's unsigned.