如何在C中做一个明确的尝试 [英] How to do an explicit fall-through in C
问题描述
较新版本的gcc提供了 Wimplicit-fallthrough
,对于大多数switch语句来说,这都是很棒的。但是,我有一个switch语句,我想允许所有案例陈述中的掉线。
The newer versions of gcc offer the Wimplicit-fallthrough
, which is great to have for most switch statements. However, I have one switch statement where I want to allow fall throughs from all case-statements.
是否有一种方法可以进行掉线?我希望避免为此文件使用 Wno-implicit-fallthrough
进行编译。
Is there a way to do an explicit fall through? I'd prefer to avoid having to compile with Wno-implicit-fallthrough
for this file.
编辑:我我正在寻找一种方法来明确掉线(如果可能的话),而不是通过编译器开关或编译指示关闭警告。
I'm looking for a way to make the fall through explicit (if it's possible), not to turn off the warning via a compiler switch or pragma.
推荐答案
使用 __ attribute__((fallthrough))
switch (condition) {
case 1: __attribute__ ((fallthrough));
case 2: __attribute__ ((fallthrough));
case 3:
printf("1..3\n");
break;
}
这篇关于如何在C中做一个明确的尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!