Article View: comp.std.c
Article #3653Re: Switch switch
From: Paul Long
Date: Fri, 04 Aug 1995 18:55
Date: Fri, 04 Aug 1995 18:55
50 lines
3382 bytes
3382 bytes
jim.fleming@bytes.com (Jim Fleming) wrote:
>Has there been any consideration given to relaxing the type requirements
>for the C switch statement expression...???
No, not that I know of. Actually, Standard C relaxed it slightly. K&R1 says
that it is an integer expression, implying truncation to int if necessary;
Standard C says that it is an integral expression, which maintains longs.
>Does anyone want to be able to write....???
[switch statement using pointer-to-character type]
It'd be kinda nice, but I thought that the restrictions on the switch statement
implied that it could be implemented with a jump table if the distribution of
case values weren't too sparse. Specifically, case labels are merely entry
points into a body of code, break statements "goto" the end of the switch
statement, the controlling expression must be an integral type, case values
must be constant expressions, and case values must be unique. This all implies
that a jump table could be used. This doesn't preclude extending the language
to allow switching on non-integral types, just that this was not the intent of
the switch statement. I don't think the switch statement will be augmented
beyond this original intention.
Another reason I don't think it'll happen is that the C language, as opposed to
the C standard library, has virtually no support for strings. The *only*
language support is that a double-quoted lexeme is idiomatic for an unnamed
object of array-of-char type, initialized with the quoted characters and
terminated with the null character. Oh, yeah, and then there's using a string
literal as an initializer--an idiom for { 'a', 'b', 'c', '\0' }, for example.
IOW, C doesn't know how to compare strings. I assume the committee will
maintain this separation between language and library regarding strings.
Yet another reason that I don't think it'll happen is illustrated by your
example--"strings" in C are ambiguous. Are you switching on the sequence of
characters pointed to by the now-degraded pointer-to-char-type expression (this
is what I assume you intended), or are you switching on the pointer value
itself?
>Are there any compiler vendors that limit the number of case statements
>to 257...which is permitted and essentially a char...???
You didn't ask, but the Rationale explains this limit: "The minimum maximum of
257 cases in a switch statement allows coding of lexical routines which can
branch on any character (one of at least 256 values) or on the value EOF." And,
no, I don't know of any implementation that uses this limit and doubt whether
there are any.
--
Paul Long 45:29:14N 122:48:09W
plong@perf.com http://www.teleport.com/~pciwww/
"Wagging head and tail, the red-tailed fish, unsupported, turns around."
Technical Details
Message-ID:
<3vtqfo$q3@news0.rain.rg.net>Path:
arc1-ca.pugleaf.net!news.sprintlink.net!psgrain!rainrgnews0!usenetReferences:
<3vn92m$oqt@News1.mcs.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.sprintlink.net!psgrain!rainrgnews0!usenet From: Paul Long <plong@perf.com> Newsgroups: comp.std.c Subject: Re: Switch switch Date: 4 Aug 1995 18:55:51 GMT Organization: Performance Computing Incorporated Message-ID: <3vtqfo$q3@news0.rain.rg.net> References: <3vn92m$oqt@News1.mcs.net> NNTP-Posting-Host: yosemite.perf.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.1 sun4c) X-URL: news:3vn92m$oqt@News1.mcs.net