switch 语句中第一个“案例"之前的代码 [英] Code before the first 'case' in a switch-statement

查看:26
本文介绍了switch 语句中第一个“案例"之前的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In C it's possible to write code before the first case label. Are there any cases for which it is useful to do this or is it just a 'dead block of code' ?

E.g.:

switch (...)    {
  {
    int a = 0x2a;
    printf("%d
", a);
  }
  case 0:
    ...
}

解决方案

It can be useful to declare variables that have a scope limited to the switch block (but be aware that any initialisers for those variables will be skipped):

switch (...)
{
    int n;

    case 0:
    ...
}

In theory, you could also put code there that you get to using goto.

这篇关于switch 语句中第一个“案例"之前的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆