"John Matthews" <[EMAIL PROTECTED]> wrote:
> "peternilsson42" <peternilsson42@> 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.
>
> ... 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?
No. The grammar for labeled statements did not change.
You can still only 'label' statements, not declarations.
Aside: This mostly causes problems where you want to jump
to the end of a block...
void foo(void)
{
int r, c;
for (r = 0; r < 3; r++)
for (c = 0; c < 3; c++)
if (...whoa...)
goto bail; /* use goto; no double-break */
bail: /* Can't! Need a statement, even if only a ; */
}
What changed with C99 was the ability to mix declarations
and statements...
C90:
compound-statement:
{ declaration-list<opt> statement-list<opt> }
declaration-list:
declaration
declaration-list declaration
statement-list:
statement
statement-list statement
C99:
compound-statement:
{ block-item-list<opt> }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
However, since statements (still) include compound-
statements you can label a new block...
case 42:
{
int labeled_block;
}
--
Peter