Article View: comp.std.c
Article #661Re: Declarations in switches, errors
From: T. William Wells
Date: Fri, 29 Sep 1989 02:28
Date: Fri, 29 Sep 1989 02:28
50 lines
1921 bytes
1921 bytes
In article <561@crdos1.crd.ge.COM> davidsen@crdos1.UUCP (bill davidsen) writes:
: In the process of looking for a totally diferent problem, I generated
: the following program:
:
: main() {
: int i = 2;
: switch (i) {
: int j = 4;
: case 1: j += 4; break;
: case 2:
: case 3:
: case 'a':
: case 'b':
: j--; break;
: }
: }
:
: Note the initialization in the 4th statement. I was unable to find any
: compiler which generated working code for this initialization (although
: one did complain that the code was not reached). I tried Sun, Ultrix,
: Xenix and gcc compilers.
:
: This is an error in the compilers! As ugly as this is, the ANSI standard
: (3.1.2.4) says "If an initialization is specified for the value stored
: in the object, it is performed on each normal entry, but not if the
: block is entered by a jump to a label."
:
: I submit that executing a switch statement constitutes "normal entry"
: and that the initialization should be performed before evaluating the
: switch variable.
dpANS 3.6.1:
"labeled-statement:
identifier : statement
case constant-expression : statement
default : statement"
Case and default are *labels*.
dpANS 3.6.4.2:
"A switch statement causes control to *jump* to, into, or past..."
(Italics mine.)
Also, I believe that K&R explicitly says that this won't work.
---
Bill { uunet | novavax | ankh | sunvice } !twwells!bill
bill@twwells.com
Technical Details
Message-ID:
<1989Sep29.022836.12674@twwells.>Path:
arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!gem.mps.ohio-state.edu!apple!motcsd!hpda!hpcuhb!hpesoc1!hp-ses!hplabs!hp-sdd!ucsdhub!sdcsvax!network.ucsd.edu!ucsd!usc!cs.utexas.edu!uunet!twwells!billReferences:
<561@crdos1>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!gem.mps.ohio-state.edu!apple!motcsd!hpda!hpcuhb!hpesoc1!hp-ses!hplabs!hp-sdd!ucsdhub!sdcsvax!network.ucsd.edu!ucsd!usc!cs.utexas.edu!uunet!twwells!bill From: bill@twwells.com (T. William Wells) Newsgroups: comp.std.c Subject: Re: Declarations in switches, errors Message-ID: <1989Sep29.022836.12674@twwells.> Date: 29 Sep 89 02:28:00 GMT References: <561@crdos1> Lines: 50