4 ms·
>To be clear, it isn't that I find the code difficult to grok: if you don't understand how switch works, please stop using C. As an aspiring C programmer, is t
by ue_ 10y ago
>To be clear, it isn't that I find the code difficult to grok: if you don't understand how switch works, please stop using C.
As an aspiring C programmer, is there anywhere I can read about interesting applications of the switch statement? Pretty much everything I've learned has come with neat little break;s and default at the end of each case.
- pkkim 10y agoProbably not for production use (as most "interesting" code isn't): https://en.m.wikipedia.org/wiki/Duff's_device https://en.m.wikipedia.org/wiki/Duff's_device
- ConceptJunkie 10y agoYou say that, but I pulled out several instances of Duff's Device from production code a few years ago, and replaced them with memcpy( ) and memcmp( ) as appropriate. This isn't 1986 any more.
- lentil_soup 10y agoIf you're an aspiring C programmer don't worry about these weird quirks. Don't believe that you need to know that to use C. A switch statement with "neat little breaks and default" is more than what you'll need 99% of the time.
- iainmerrick 10y agoThe remaining 1% is a big comment reading "/* FALL THROUGH */" instead of a break. Once you know what break does, you should know what happens when you leave it out (and why you might want to). The remaining 0% is stuff like Duff's Device.
- geofft 10y agoA switch statement is just a bunch of gotos who have hurriedly put on an ill-fitting suit and tie to look respectable. Any context where you would want "if x == this, goto here; if x == that, goto there" is a potential place to use the switch statement. Duff's device is a good example, but there are many things that can be built this way.
- dllthomas 10y ago> Any context where you would want "if x == this, goto here; if x == that, goto there" is a potential place to use the switch statement. Only if "here" and "there" are later on in the same block.
- paulrpotts 10y agoIf you want to read about horrible but entertaining C hacks, check out the entries in the International Obfuscated C Code Contest, the book that came out of the contests a few years back, or books like Deep C Secrets. http://www.ioccc.org/ http://www.ioccc.org/ As an developer who has written a lot of shipping code in C and C++ for almost 30 years, I would strongly advise you to write your switch statements in as straightforward a way as possible. If you use fall through, place very noticeable /* FALL THROUGH */ comments to indicate that you meant to do that. If you wind up doing some kind of oddball thing because you _have confirmed_ that it results in significantly better optimization for a specific hardware target and compiler, comment the crap out of that, maybe make it conditional with an #if block, and (what I prefer to do is) write the simple version too, test both versions, and keep the simple version as a comment, so a future maintainer can verify that the complicated code is doing what you intended.