logoalt Hacker News

xg15 • today at 7:18 AM • 1 reply • view on HN

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

As someone who only writes C or C++ occasionally, I'd consider this a good submission for an obfuscated C contest.

It really does look simple at first glance.

Then you start to wonder what the stop condition is.

Then you realize the i-- is the stop condition.

Then you wonder how the whole thing works at all.

(The description explained the trick well, and the construct makes sense once you understand it, but I still think this is one example of something that is "too simple" and becomes cryptic)

After all the stuff you hear about undefined behavior, I'm also honestly surprised that wraparound on unsigned types is not undefined behavior.


Replies

anal_reactor • today at 7:35 AM

Hard disagree. Obfuscated code is code that does something different from what it seems to be doing at first glance. This backwards loop does exactly the thing you'd expect it to do - in code review I'd only double-check whether it's actually correct - I can see someone making a mistake here and creating an infinite loop.

> I'm also honestly surprised that wraparound on unsigned types is not undefined behavior.

I consider it common knowledge TBH

➕ show 2 replies