--- In [email protected], "peternilsson42" <[EMAIL PROTECTED]>
wrote:
>
> "John Matthews" <jm5678@> wrote:
> >
> > Hi- gcc by default allows variables to be declared in
> > the middle of code
> 
> This is permitted by C99 as well as C++. For (partial)
> C99 conformance, use -std=c99.
> 
> > But it doesn't appear to like them directly after case
> > statements
> 
> The standard C grammar for a labeled statement is:
> 
>   labeled-statement:
>     identifier : statement
>     case constant-expression : statement
>     default : statement
> 
> A declaration is not a statement.
> 
> > But if I insert an empty statement before the int
> > it's fine.
> 
> Yes, because that fits the grammar of C99. Although,
> it doesn't fit the grammar of C90 (-ansi -pedantic)
> since that does not permit the mixing of declarations
> after a statement (beyond nested blocks).
> 
> > I'm using gcc 3.4.6 under linux - is this a
> > peculiarity of this compiler, or version of
> > the compiler, or something else?
> 
> GNU C is not standard C. If you use the default mode,
> you're using GNU C. So, technically it is a peculiarity
> of the compiler.
> 
> That said, you should be trying to write code that
> conforms to the core C language wherever possible.

Thanks Peter. Sorry, can I just clarify - if my code (with the
variable declaration immediately after the case) was being compiled by
a conformant C99 compiler (in C99 mode), it would be ok?

Yes, I know it's not good code, and I've avoided the issue and
hopefully improved things (ie. at least made it C90 compliant) by
enclosing the code inside the case in braces. I'm just curious.

John

Reply via email to