[Bug c/21689] New: Variable declaration after switch case expression causes compile error

matt_hicks at bose dot com gcc-bugzilla@gcc.gnu.org
Fri May 20 23:11:00 GMT 2005


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
}

-- 
           Summary: Variable declaration after switch case expression causes
                    compile error
           Product: gcc
           Version: 3.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: matt_hicks at bose dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: gcc exmple.c


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21689



More information about the Gcc-bugs mailing list