🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #215

Re: Taming The Switch-Statement

#215
From: bart
Date: Sun, 11 Oct 2026 13:39
87 lines
5327 bytes
On 11/10/2026 13:42, David Brown wrote:
> On 11/10/2026 12:58, bart wrote:


>> We don't know that, we just see the end result.
> 
> I don't think there is anything much you can say about that without 
> sidetracking to a definition of what we mean by "a problem", and then 
> doing a massive survey of lots of C programmers.
> 
> We do know that C is a massively successful programming language, used 
> by vast numbers of people, and that switch statements are common in C. 
> This is true regardless of any real or imagined "problem" with C's 
> switch statements.

I know that, despite the success, a vast amount of effort has to be 
spent to keep on top of or to get around all its problems, and there may 
yet be bugs we don't know about due to them.

I can see that from looking at other forums at the things people have 
trouble with, that wouldn't happen with a tidier and less quirky language.

> 
>>
>>>   There are, however, occasional exceptions and more complicated 
>>> code.  Such use-cases would need to be handled in other ways if C's 
>>> switch did not work as it does.  My gut feeling is that a solid 
>>> proportion of such cases were more relevant in the old days before 
>>> more optimising compilers - things like Duff's Device spring to mind 
>>> as being useful long ago, but unnecessary with modern tools.
>>
>> This is the core C code for Duff's Device from Wikipedia:
>>
> 
> 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.

Yet there are other ways to express the same thing using other features.

(BTW gcc gives me fall-through warnings on that Duff's example. How do 
you shut that up while keeping them for other code?)

>> That should tell you more.
> 
> We could pick a dozen different real-world languages, with a dozen 
> different kinds of switch, case, match or other multiple choice 
> construct (or lack thereof).  That should tell you that there is no 
> "right" or "wrong" choice here - there are different design choices, 
> with different pros and cons, different flexibilities and limitations, 
> and appropriate for different kinds of language and different kinds of 
> tools.

This is a line from my C compiler written in my language:

	doswitch lxsptr++^
	when 'A'..'Z', 'a'..'z', '$', '_' then ...

This is what I'd have to write to express the same thing in standard C:

   while (1)
    switch (*lxsptr++) {
    case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
    case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
    case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
    case 'V': case 'W': case 'X': case 'Y': case 'Z':
    case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
    case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
    case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
    case 'v': case 'w': case 'x': case 'y': case 'z':
    case '0': case '1': case '2': case '3': case '4': case '5': case '6':
    case '7': case '8': case '9': case '_': case '$': ...

So, what are the pros here? As I can't see any, only cons.

> I suggest you find something better to spend your time on than re-hashes 
> of your obsessions about what you don't like about C.

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.

Technical Details
Message-ID: <11ag3ia$2m2aj$1@dont-email.me>
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!.POSTED!not-for-mail
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>

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!.POSTED!not-for-mail
From: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Sun, 11 Oct 2026 14:39:23 +0100
Organization: A noiseless patient Spider
Lines: 87
Message-ID: <11ag3ia$2m2aj$1@dont-email.me>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Oct 2026 13:39:22 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2820435"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX189Qx+VlnBsIeZ53bQ297ZA"; posting-host="bb4c63ed27c528dcfc498e66a186f254"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:jvZRJwtm8xM31JNp4aBZqn+WTxQ= sha256:m2o9Lintp+pjjAZM82moWsHvDh1egdU2eZrmvG6h0/o=
	sha1:DXGUkf8nzzJWmoyKAsEefm0FFeA= sha256:ac4Zo9x2teSQwdUjZmkgS6CKUFQfGO6U9RxxxR2ISpI=
Content-Language: en-GB
In-Reply-To: <11ag08f$2k9do$1@dont-email.me>