Article View: comp.lang.c
Article #151Re: Taming The Switch-Statement
From: Keith Thompson
Date: Sat, 10 Oct 2026 22:54
Date: Sat, 10 Oct 2026 22:54
44 lines
3347 bytes
3347 bytes
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.
--
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:
<11aefo0$24k8m$2@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> <20261010195051.00000594@yahoo.com> <11adv82$cjlv$2@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: Sat, 10 Oct 2026 15:54:56 -0700 Organization: None to speak of Lines: 44 Message-ID: <11aefo0$24k8m$2@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> <20261010195051.00000594@yahoo.com> <11adv82$cjlv$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sat, 10 Oct 2026 22:54:57 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="2248982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+QqZ3vtnpGjRJwn1PrRq+h"; posting-host="29efadadb268dc6914862df94b2a05d4" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:jQQW5Z5TK/AnNXwAxcpuFjQBRdU= sha256:iGeiGg7Ho97/oSC7ksLcGYWp0zsv9O/HWKkwxVK4rCk= sha1:q7D1qA/GoECtBkgkDUgKSkb8xWA= sha256:/g5+2xKfc5JQxyhSNlACa7pQLLNtjVKShfQeHDgqbRM=