"John Matthews" <[EMAIL PROTECTED]> wrote:
>
> Hi- gcc by default allows variables to be declared in
> the middle of code eg.
>
> int test(int x)
> {
> x++;
> int y = x + 2; /* ok */
> return y;
> }
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:
>
> int test(int x)
> {
> x++;
> int y = x + 2; /* ok */
> switch (x)
> {
> case 2:
> int z = x; /* error here */
>
> error: syntax error before "int"
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:
>
> case 2:
> ; int z = x; /* ok now */
>
> 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.
That way you have less to worry about with regards
to quirky compilers.
--
Peter