switch 语句中的独立中断

standalone break within a switch statement

我偶然发现了这段代码,它按预期工作:

switch (ev->deviceType) {

    break;

    case DEVICE_TS1E0:
        //some code
        break;

    case DEVICE_TS1E3:
       //some code
       break;

    default:
        //some logging
        break;
}

现在,切换开始时有一个寂寞的break;,似乎没有效果。

在任何情况下 break; 会产生影响吗?

该语句以及任何其他不在 switch 语句中的 case 子句中的语句都是无法访问的代码,也称为死代码。这意味着他们无论如何都不会 运行 。不建议使用。

TL;DR break 语句是无效的并且是死代码。控制永远不会到达那里。

C11 标准有一个类似案例的很好的例子,让我直接引用。

来自章节 §6.8.4.2/7,(强调我的

EXAMPLE In the artificial program fragment

switch (expr)
{
    int i = 4;
    f(i);
case 0:
    i = 17;
    /* falls through into default code */
default:
    printf("%d\n", i);
}

the object whose identifier is i exists with automatic storage duration (within the block) but is never initialized, and thus if the controlling expression has a nonzero value, the call to the printf function will access an indeterminate value. Similarly, the call to the function f cannot be reached.