Article View: comp.lang.c
Article #165Re: Taming The Switch-Statement
From: Janis Papanagnou
Date: Sun, 11 Oct 2026 01:41
Date: Sun, 11 Oct 2026 01:41
53 lines
3874 bytes
3874 bytes
On 2026-10-11 00:54, Keith Thompson wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>> On 2026-10-10 18:50, Michael S wrote:
>>> I fully agree with your post (plus have to add that "more structured
>>> switch" apart from new keyword(s) will need some new way to express
>>> common handler for several cases. Ada/VHDL solution of using | would
>>> not work in C for obvious reasons).
>>
>> Pascal allows '..' and ',' (for ranges and lists), and even mixed.
>> That should (in principle) be a syntactical possibility in "C".
>
> C2Y will add case ranges using "..." (which is already a token used for
> variadic functions), with syntax and semantics close to the existing gcc
> extension.
>
> switch (foo) {
> case 'a' ... 'z':
> // ...
> }
>
> This was considered and rejected (with "..") for C89. One reason for
> rejecting it was that 'A'..'Z' is not contiguous in all character
> sets (particularly not in EBCDIC). The C89 Rationale also mentions
> the problem of large code or jump table space for something like
> 0..65535, something that can be handled these days by minimally
> clever compilers.
>
> One problem is that `case 0...1:` is a syntax error. 0...1 is a valid
> preprocessing number that can't be converted to a valid constant.
> The solution is to surround the ... with spaces, which is probably
> a good idea anyway.
>
> As for ',', that would avoid having to repeat the "case" keyword,
> but it could be visually confusing. `case 0, 1:` is currently a
> syntax error, but only because a constant expression can't include
> a comma operator (with some obscure exceptions). Allowing commas
> would prevent a future version of the language from either allowing
> comma operators in constant expressions or permitting non-constant
> expressions in case labels. I still wouldn't mind such a change,
> but it would add no real expressive power and I don't think anyone
> has proposed it.
Thanks for the interesting background information.
Your explanation shows how difficult it is to add features (even if
they are comparably simple appearing ones) to an existing language;
many things have to be considered, interdependencies, and also an
educated thought about future consequences.
It's certainly a lot easier if you can design a language from scratch.
Janis
Technical Details
Message-ID:
<11aepgb$286ml$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-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> <20261010195051.00000594@yahoo.com> <11adv82$cjlv$2@dont-email.me> <11aefo0$24k8m$2@kst.eternal-september.org>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: Janis Papanagnou <janis_papanagnou+ng@hotmail.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sun, 11 Oct 2026 03:41:31 +0200 Organization: A noiseless patient Spider Lines: 53 Message-ID: <11aepgb$286ml$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> <20261010195051.00000594@yahoo.com> <11adv82$cjlv$2@dont-email.me> <11aefo0$24k8m$2@kst.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 11 Oct 2026 01:41:32 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2366165"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Ilj1GuEQV7bEVeJQXK71v"; posting-host="dc047fa64def26b023d01b53ecdcc125" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:UMsbsAH8EJDSOu5Tk/neP6i7Pj8= sha256:Sr+ZmYEbXFNYXX4ZL+urkNykSxadvPono4wc6UksQvc= sha1:hzBd3v0hhsLcKE3NL0V7OQxTH7U= sha256:DRoWp7x6B6ZdygfU7jqTlfNkvnSiQB4ERBo9o8taRZY= Content-Language: en-US In-Reply-To: <11aefo0$24k8m$2@kst.eternal-september.org>