Declarations in switches, errors

Tom Karzes karzes at mfci.UUCP
Fri Oct 13 14:07:13 AEST 1989


In article <10888 at riks.csl.sony.co.jp> diamond at riks. (Norman Diamond) writes:
->>I submit that executing a switch statement constitutes "normal entry"
->No, a switch statement is entered by a jump to a label.
-
-Mr. Kremer, you start your reply with "No."  So you think that correct
-execution of a switch statement constitutes abnormal entry to the block.
-I conjecture that very few will agree with you.
-
-Otherwise, it seems that both posters are correct.  Executing a switch
-statement is both normal entry and a jump to a label.  The standard is
-self-contradictory.

For Pete's sake, this silly discussion was resolved long ago.  Let it die.
The answer is very simple; you just don't like it.  A switch statement
in C takes a single subordinate statement.  It can be ANY statement.  Any
case or default labels within that statement which are not associated with
a more closely nested switch statement are associated with that switch
statement.  That's ALL you need to know to figure out the rest.  Typical
use of a switch statement (and indeed the only use with which most people
are familiar) is to use a block as the subordinate statement, with all
case labels, and the default label (if present), at the top level inside
the block.  In this case, execution of the switch statement causes a branch
into the block in the non-fall-through case, therefore the block is NOT
entered "normally" and any automatic initializations contained within
that block will be skipped.  To do otherwise would be extremely unintuitive.
All kinds of special exceptions about entering blocks via a switch statement
would have to be added, and the implementation would be dreadful.  If no
default label was present, the compiler would first have to evaluate the
expression and determine whether it matched one of the case labels.  If
so, it would then have to branch to the start of the block and execute any
automatic initializations.  Then it would have to actually branch to
the appropriate case label.  It's an absurd proposal, and is extremely
unintuitive to anyone with even an average grasp of C.  When I first
learned C many years ago, I didn't initially realize that switch
statements could take any type of subordinate statement, and in particular
that the subordinate statement could be a block that contained declarations,
some of which might be automatics with initializers.  When I did learn about
this, it was OBVIOUS that executing the switch statement would blow right
past any such initializers.  Perhaps what you really don't like is the
term "normal entry", because "abnormal entry" is somehow "bad", which
implies that typical use of a switch statement is "bad".  Fine.  Think of
it as "fall-through entry", or "normal-but-non-switch entry", or whatever
pedagogical aid seems appropriate to you.  No one ever claimed that C was
an easy language for beginners.  And please post any further speculation
as to how switch statements ought to be redesigned to comp.lang.misc.
The members of the ANSI committee weren't total morons, you know.



More information about the Comp.std.c mailing list