🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #197

Re: Taming The Switch-Statement

#197
From: David Brown
Date: Sun, 11 Oct 2026 09:41
69 lines
4861 bytes
On 11/10/2026 03:41, Janis Papanagnou wrote:
> 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.
> 

The comma operator turns up often as a hinder to syntax extension in 
C-like languages - disproportional, I think, to its usage in "typical" C 
code.  As a clear example, C++ had to deprecate its usage in subscript 
operators so that they could later support a multi-dimensional subscript 
operator.  "m[x, y]" meant "evaluate x, throw away the result, then 
evaluate m.operator[](y)" instead of the more useful "m.operator[](x, y)".

Still, C has the comma operator, some people like it, some code uses it, 
and it is not going anywhere.  And it would be nice if C can continue to 
avoid C++ style special exception rules.

> It's certainly a lot easier if you can design a language from scratch.
> 

Yes.  Or if you can, like Bart, just change your language to suit, or 
put in whatever inconsistencies and ambiguities you like because you 
know exactly how programmers will use the language.
Technical Details
Message-ID: <11aflk7$2g8ft$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> <20261010195051.00000594@yahoo.com> <11adv82$cjlv$2@dont-email.me> <11aefo0$24k8m$2@kst.eternal-september.org> <11aepgb$286ml$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: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Sun, 11 Oct 2026 11:41:27 +0200
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <11aflk7$2g8ft$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> <11aepgb$286ml$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 09:41:28 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2630141"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+vT4yXMikjKyb5Nilb5rpiQPEiBTl3Z4A="; posting-host="10283a9df1f0548a34352f02c4ddf11b"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:CdZaNtTqY6kXKrwkn+gomFd6ztw= sha256:sMDP5/8UBV2SfZldd5MGf6Gq3v4/I12bxsMZEo8EmS8=
	sha1:SqyDnN/b6NfKjdeh9o9B03Zaox4= sha256:StTQHIwQTuUKxrXGt1nStsciBzPOYQok1L+LU/ydVz8=
In-Reply-To: <11aepgb$286ml$1@dont-email.me>
Content-Language: en-GB