Bug 21689 - Variable declaration after switch case expression causes compile error
Summary: Variable declaration after switch case expression causes compile error
Status: RESOLVED DUPLICATE of bug 7508
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.3.3
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-05-20 23:10 UTC by Matt Hicks
Modified: 2005-09-15 14:23 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build: gcc exmple.c
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Hicks 2005-05-20 23:10:44 UTC
If there is a variable declaration as the first statement after the colon at the
end of a case expression, the compiler returns a syntax error. If there any
statements between the case expression and the variable declaration then it will
not produce an error, a partial list of the statements that I have found that
will prevent an error are as follows:

A void statement i.e. ((void) 0);
Using an un-necessary code block i.e. { code}
An empty line with nothing but a semi-colon ;
Any function call

It seems as long as there is SOMEHING in between the case expression and the
variable declaration it is fine.

See example code below:

--- example.c ---
void foo0(void)
{
    int test = 0;
    
    switch (test)
    {
    case 0:
        int bar = 1; //error here: " example.c:8: error: syntax error before "int""
        break;
    default:
        //
        break;
    }
}

void foo1(void)
{
    int test = 0;
    
    switch (test)
    {
    case 0:
        int bar; //error here: " example.c:23: error: syntax error before "int""
        bar = 1; //error here: " example.c:24: error: `bar' undeclared (first
use in this function)"
        break;
    default:
        //
        break;
    }
}

typedef int UINT; //typedef for below

void foo2(void)
{
    int test = 0;
    
    switch (test)
    {
    case 0:
        UINT bar = 1; //error here: "example.c:41: error: syntax error before "bar""
        break;
    default:
        //
        break;
    }
}

void foo3(void)
{
    int test = 0;
    
    switch (test)
    {
    case 0:
        ((void) 0); //void statement
        int bar = 1; //works
        break;
    default:
        //
        break;
    }
}

void foo4(void)
{
    int test = 0;
    
    switch (test)
    {
    case 0:
        { //code block
            int bar = 1; //works
        }
        break;
    default:
        //
        break;
    }
}

void foo5(void)
{
    int test = 0;
    
    switch (test)
    {
    case 0:
        ; //empty line
        int bar = 1; //works
        break;
    default:
        //
        break;
    }
}

void foobar(void); //function prototype for below

void foo6(void)
{
    int test = 0;
    
    switch (test)
    {
    case 0:
        foobar(); //function call
        int bar = 1; //works
        break;
    default:
        //
        break;
    }
}

void foobar(void) //dummy function
{
    //nada
}
Comment 1 Falk Hueffner 2005-05-20 23:20:13 UTC
(In reply to comment #0)
> If there is a variable declaration as the first statement after the colon at the
> end of a case expression, the compiler returns a syntax error.

That is exactly as expected. After the label, there needs to be a statement,
and declarations are not statements.
Comment 2 Andrew Pinski 2005-09-15 14:23:07 UTC
Reopen to ...
Comment 3 Andrew Pinski 2005-09-15 14:23:44 UTC
Mark as a dup of bug 7508.

*** This bug has been marked as a duplicate of 7508 ***