[comp.lang.c] Rules in placement of 'case'-labels

risto@tuura.UUCP (Risto Lankinen) (10/16/90)

Hi!

I've found that the following construct compiles without errors at least
in Microsoft C version 6.0 (sorry, the scandinavian keyboard inhibits some
crucial characters in favor to special alphabets) :

DoThis( ... )
(
   switch( ... )
   (
   case 1:
      for( ... )
      (
         DoSomething1();
         /* Note, the 'case' label below is inside an open statement block */
   case 2:
         DoSomething2();
      )
      break;
   ...
   )
)

Weird, isn't it?  It also made me wonder whether it is legal in C .  I can
think of a few practical uses for it in Windows programming, where code size
is a concern.

However, if this is a legal construct, it introduces an inconsistency:  You
can replace the 'for' by 'if', 'while', 'do' or just a simple statement block
without any flow-control keyword.  But you cannot substitute another 'switch'
in similar manner, because the label will then belong to the inner of the two
switches.

Terveisin: Risto Lankinen
-- 
Risto Lankinen / product specialist ***************************************
Nokia Data Systems, Technology Dept *  2                              2   *
THIS SPACE INTENTIONALLY LEFT BLANK * 2 -1 is PRIME!  Now working on 2 +1 *
replies: risto@yj.data.nokia.fi     ***************************************

karl@haddock.ima.isc.com (Karl Heuer) (10/19/90)

In article <808@tuura.UUCP> risto@tuura.UUCP (Risto Lankinen) writes:
>I've found that the following construct compiles without errors at least
>in Microsoft C version 6.0 [example where case labels are misbalanced with
>respect to braces]...  It also made me wonder whether it is legal in C.

It's legal in both Classic and ANSI C, though some compilers have been known
to reject it.  It's also highly unstructured; use it with the same level of
caution as a `goto'.

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint