如何在 C 中的 switch case 语句中使用范围? [英] How can I use ranges in a switch case statement in C?
本文介绍了如何在 C 中的 switch case 语句中使用范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的逻辑是:
if number is between 1 to 10, execute first case statement
if number is from 20 to 30, execute second case statement
除了下面的解决方案之外还有其他解决方案吗?
is there a solution other than the one below?
case '1' ... '10':
case '20' ... '30':
推荐答案
The GCC compiler supports, as a language extension, case ranges like:
switch(i) {
case 0 ... 9: return true;
default: return false;
}
Clang/LLVM 也接受此语言扩展.因此,如果您负担得起将代码限制为 GCC & 的话,请使用它.Clang 编译器.
This language extension is also accepted by Clang/LLVM. So use it if you can afford restricting your code to GCC & Clang compilers.
另见这个.
我不知道为什么这个扩展没有包含在 C11 标准中.
I have no idea why this extension was not included in C11 standard.
这篇关于如何在 C 中的 switch case 语句中使用范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文