Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!rutgers!ucla-cs!zen!ucbvax!decvax!ima!haddock!karl From: karl@haddock.ISC.COM (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Switch statements Message-ID: <1078@haddock.ISC.COM> Date: Wed, 9-Sep-87 12:54:07 EDT Article-I.D.: haddock.1078 Posted: Wed Sep 9 12:54:07 1987 Date-Received: Fri, 11-Sep-87 04:34:05 EDT References: <855@tjalk.cs.vu.nl> <2683@hoptoad.uucp> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 26 In article <2347@mmintl.UUCP> franka@mmintl.UUCP (Frank Adams) writes: >One occasional problem with C, which does not fit with the overall language >philosophy, is that a range check is always[1] implicit in a switch >statement. If the statement happens to be at a key place in the code, a not >unusual occurrence, this can add significantly to running time. > >Does anyone have any suggestions for a good syntax to indicate to the >compiler that this check is not desired; that if the discriminant is not one >of the specified cases, the result of the switch is to be undefined? How about "#pragma nodefault"? I've noticed this too, but (unlike you) I don't think it would be such a bad thing if this were the default behavior. Learning to "always" add the line "default: break;" isn't any harder than remembering to "always" have break statements between cases. (Not to say that two wrongs make a right -- I'm just trying to prebut the argument that novice users wouldn't be able to cope with it.) Moreover, PASCAL -- one of the languages to which C is oft compared, and often the only previous structured language known to a new C user -- has this same attribute. (It's true that PASCAL's case statement is crippled beyond usefulness, but that's because there's no way to override this behavior. This would not be the case in the hypothetical modified C.) Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint