Article View: comp.lang.c
Article #245Re: Taming The Switch-Statement
From: Keith Thompson
Date: Sun, 11 Oct 2026 22:35
Date: Sun, 11 Oct 2026 22:35
61 lines
4018 bytes
4018 bytes
bart <bc@freeuk.com> writes:
> On 11/10/2026 13:42, David Brown wrote:
[...]
>> We know what Duff's Device is, and no one cares about your
>> language. It is not an alternative to C in any way.
>
> It doesn't matter. The point is that Duff's Device is always given as
> an example to show how wonderful it is to have default fall-through in
> C, even though in the vast majority of cases, 'break' has to be used
> to inhibit it.
No, it is not.
Here's what Tom Duff himself wrote about it in 1988:
Disgusting, no? But it compiles and runs just fine. I feel
a combination of pride and revulsion at this discovery. If no
one's thought of it before, I think I'll name it after myself.
It amazes me that after 10 years of writing C there are still
little corners that I haven't explored fully. (Actually, I have
another revolting way to use switches to implement interrupt
driven state machines but it's too horrid to go into.)
Many people (even bwk?) have said that the worst feature of C is
that switches don't break automatically before each case label.
This code forms some sort of argument in that debate, but I'm
not sure whether it's for or against.
https://doc.cat-v.org/bell_labs/duffs_device
> Yet there are other ways to express the same thing using other features.
Are there? Duff himself was unable to find a more efficient
algorithm -- and this was in a (rare) context where efficiency
was more important than clarity.
> (BTW gcc gives me fall-through warnings on that Duff's example. How do
> you shut that up while keeping them for other code?)
The ways to inhibit gcc's warnings about implicit fallthrough have
already been discussed in this thread. But you've said before
that you don't care about gcc, so I won't try yet again to teach you
about how to use it.
[...]
> Actually it is fascinating seeing people defend ludicrously bad (to
> say nothing of unergonomic) language features like this, and
> vigorously reject any suggestions of improvement.
When someone here explains how something works, you think they're
vigorously defending it.
Yes, the implicit fallthrough in C's switch statements a misfeature.
I do not support suggestions to "fix" it because no such fix would
actually improve the language.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Technical Details
Message-ID:
<11ah2v2$329bt$1@kst.eternal-september.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11admri$1s1lb$1@dont-email.me> <11afq4b$2idte$1@dont-email.me> <11ag08f$2k9do$1@dont-email.me> <11ag3ia$2m2aj$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson <Keith.S.Thompson+u@gmail.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sun, 11 Oct 2026 15:35:14 -0700 Organization: None to speak of Lines: 61 Message-ID: <11ah2v2$329bt$1@kst.eternal-september.org> References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11admri$1s1lb$1@dont-email.me> <11afq4b$2idte$1@dont-email.me> <11ag08f$2k9do$1@dont-email.me> <11ag3ia$2m2aj$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Sun, 11 Oct 2026 22:35:16 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="3220861"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18RtWRYiquecY4+bBW4gZ9q"; posting-host="50698cca2518b68a86663845e71c144a" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:2f+MO1yRrVNEshjhboDjYNOgWM4= sha256:iZIK/rofmA/qiwh++yr1ZalKqmc1Ci88yJGiDppWSyI= sha1:eMnmVT4/Hg+PIeZdZlHeN+jbiuM= sha256:PS8nwCV6KhvOT8W5zvp/Z0vn/zaWVPcBfjIGJ09stKQ=