On Sun, 25 Jul 1999, Mike Davis wrote:
> Brace Requirements:
> 
> I have been using Switch/Case statements forever.  But I found an 
> example that makes me wonder if I have misunderstood something.
> 
> I get a compile error with the below code if I don't use braces as
> shown in the 'case' statement.  I know it's because of theChr 
> declaration but I don't understand why or how the braces fixes the 
> problem.  I have never used braces before in case statements.
> 
> What's up?

  most C compilers will not allow you to declare a variable 
  unless you declare it at the beginning of some scope.

  when you put a '{' there, you are declaring a new block
  scope, so the compiler is more than happy to accept it.

  without it, the scope is the case statement.. and since you
  already have statements before this declaration - it barfs.

  GOOD:                         BAD:

    switch (myVar)                switch (myVar) 
    {                             {
      case a:                       case a:
             {                              // declaration
               // declaration     }
             }
    }

  this stuff is 'standard' C stuff.. some compilers are however
  leniant on this rule.. they let you get away with it :>

  gcc for sure will not. 

  i believe it is in the ANSI specification.

az. 
--
Aaron Ardiri 
Lecturer                       http://www.hig.se/~ardiri/
University-College i G�vle     mailto:[EMAIL PROTECTED]
SE 801 76 G�vle SWEDEN       
Tel: +46 26 64 87 38           Fax: +46 26 64 87 88
Mob: +46 70 352 8192           A/H: +46 26 10 16 11

Reply via email to