开发者

declare variable in switch? [duplicate]

开发者 https://www.devze.com 2023-02-04 19:17 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Why can't variables be declared in a switch statement?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Why can't variables be declared in a switch statement?

switch (i){
 case 'i': int i; break; } //it works


switch (i){
 case 'i': int i;i=0; break; } //it also works


switch (i){
 case 'i': 开发者_如何学Pythonint i=0; break; } //it ain't


Dunno the error, but this also works for me in Objective-C (strict superset of C, although I don't believe that 1) and I do this always, even when I simply return something:

switch (i) {

  case 'i': { // <- curly brackets
    int i = 0; break;
  } // <- curly brackets

}

(1 Because a variable can be named id in C but not in Objective-C)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号