Why can't variables be declared in a switch st

2018-12-31 00:51发布

I've always wondered this - why can't you declare variables after a case label in a switch statement? In C++ you can declare variables pretty much anywhere (and declaring them close to first use is obviously a good thing) but the following still won't work:

switch (val)  
{  
case VAL:  
  // This won't work
  int newVal = 42;  
  break;
case ANOTHER_VAL:  
  ...
  break;
}  

The above gives me the following error (MSC):

initialization of 'newVal' is skipped by 'case' label

This seems to be a limitation in other languages too. Why is this such a problem?

23条回答
宁负流年不负卿
2楼-- · 2018-12-31 01:45

After reading all answers and some more research I get a few things.

Case statements are only 'labels'

In C, according to the specification,

§6.8.1 Labeled Statements:

labeled-statement:
    identifier : statement
    case constant-expression : statement
    default : statement

In C there isn't any clause that allows for a "labeled declaration". It's just not part of the language.

So

case 1: int x=10;
        printf(" x is %d",x);
break;

This will not compile, see http://codepad.org/YiyLQTYw. GCC is giving an error:

label can only be a part of statement and declaration is not a statement

Even

  case 1: int x;
          x=10;
            printf(" x is %d",x);
    break;

this is also not compiling, see http://codepad.org/BXnRD3bu. Here I am also getting the same error.


In C++, according to the specification,

labeled-declaration is allowed but labeled -initialization is not allowed.

See http://codepad.org/ZmQ0IyDG.


Solution to such condition is two

  1. Either use new scope using {}

    case 1:
           {
               int x=10;
               printf(" x is %d", x);
           }
    break;
    
  2. Or use dummy statement with label

    case 1: ;
               int x=10;
               printf(" x is %d",x);
    break;
    
  3. Declare the variable before switch() and initialize it with different values in case statement if it fulfills your requirement

    main()
    {
        int x;   // Declare before
        switch(a)
        {
        case 1: x=10;
            break;
    
        case 2: x=20;
            break;
        }
    }
    

Some more things with switch statement

Never write any statements in the switch which are not part of any label, because they will never executed:

switch(a)
{
    printf("This will never print"); // This will never executed

    case 1:
        printf(" 1");
        break;

    default:
        break;
}

See http://codepad.org/PA1quYX3.

查看更多
不流泪的眼
3楼-- · 2018-12-31 01:45

Try this:

switch (val)
{
    case VAL:
    {
        int newVal = 42;
    }
    break;
}
查看更多
妖精总统
4楼-- · 2018-12-31 01:46

You can declare variables within a switch statement if you start a new block:

switch (thing)
{ 
  case A:
  {
    int i = 0;  // Completely legal
  }
  break;
}

The reason is to do with allocating (and reclaiming) space on the stack for storage of the local variable(s).

查看更多
浅入江南
5楼-- · 2018-12-31 01:47

New variables can be decalared only at block scope. You need to write something like this:

case VAL:  
  // This will work
  {
  int newVal = 42;  
  }
  break;

Of course, newVal only has scope within the braces...

Cheers, Ralph

查看更多
何处买醉
6楼-- · 2018-12-31 01:48

I believe the issue at hand is that is the statement was skipped, and you tried to use the var elsewhere, it wouldn't be declared.

查看更多
登录 后发表回答