標籤:

C語言中case後真的不能跟範圍?

昨天有個小夥伴問我想用case判斷一個範圍咋辦,我以前覺得case後貌似只能跟一個數字或字元啥的,沒見過case後跟範圍呀。於是我查了一下資料,都說case後不能跟範圍,包括c primer plus也是這樣說。

然而,我在一個台灣同胞的博客里發現了如下代碼:

#include &

#include &

int main(int argc, char *argv[]) {

int score;

printf("阿獃,你考幾分?");

scanf("%i", score);

switch(score){

//自訂區域 case(空格)90...(空格)100 表示 90~100

case 90 ... 100:

printf("level A");

break;

case 80 ... 89:

printf("level B");

break;

case 70 ... 79:

printf("level C");

break;

case 60 ... 69:

printf("level D");

break;

case 0 ... 59:

printf("不及格唷");

break;

default :

printf("亂來!!亂輸入!!");

break;

// 可以寫成區域,程式進入方法的括號會配置記憶體,離開括號後會釋放記憶體

// case xxx:{

// int abc;

// }break;

}

return 0;

}

很驚悚,可以運行。。。

一臉蒙逼。。什麼情況?

平台,codeblocks(GCC)


gnu c擴展可以在case語句後面跟一個範圍

Using and Porting the GNU Compiler Collection (GCC): C Extensions

You can specify a range of consecutive values in a single case label, like this:

case low ... high:

This has the same effect as the proper number of individual case labels, one for each integer value from low to high, inclusive.

This feature is especially useful for ranges of ASCII character codes:

case "A" ... "Z":

Be careful: Write spaces around the ..., for otherwise it may be parsed wrong when you use it with integer values. For example, write this:

case 1 ... 5:

rather than this:

case 1...5:


你以為你寫pascal啊,用if


本身是不可以的,當然如果僅僅從技術角度討論這個問題的話,C99 用case倒是可以實現邏輯上的範圍判定,不加break就好了。舉個例子:

case 3: break;
case 4:
case 5:
case 6:
//Todo
break;
default:

實現了一個判斷int從4到6的range。

我試了下,這是可以運行的。


多寫幾個case然後fallthrough不就完了么……


c 裡面的switch不是用來給你做語法糖的。

明明可以用 if else 解決的語句為什麼要專門設計一個語句出來?

因為switch可以幫助編譯器將其實現成jump table來提高性能。所以才會要求只能switch整型。


你可以先用switch case把單值的分支枚舉完,然後在default分支下用if枚舉範圍的情況


幾個答主怎麼連題都不看完就開答。。。

c spec沒寫,編譯器可以自己做啊。反正就是查個表+eq,gt,lt的邏輯運算么

https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html

Next: Cast to Union, Previous: Designated Inits, Up: C Extensions

6.28 Case Ranges

You can specify a range of consecutive values in a single case label, like this:

case low ... high:

This has the same effect as the proper number of individual case labels, one for each integer value from low tohigh, inclusive.

This feature is especially useful for ranges of ASCII character codes:

case "A" ... "Z":

Be careful: Write spaces around the ..., for otherwise it may be parsed wrong when you use it with integer values. For example, write this:

case 1 ... 5:

rather than this:

case 1...5:


如輪子哥所說 你用了帶私貨的編譯器.....你要這樣搞的話....世界都是你的 年輕人......你以為編程語言的語法是哪裡來的? 不就是一幫人定義了規則....一堆大神按照規則寫出了編譯器嗎....有的編譯器實現了超集....所以世界都是你的


標準C是不支持的,GNU C對標準作了擴展,支持case的值是一個範圍。

使用格式為:

case 1 ... 3:
printf("%d
",i);
break;

具體代碼,看截圖:


如果是這題的這種範圍的話,應該可以switch score除個10再case,如果要更一般的方法的話,c標準應該不允許。


如果用非標擴展,有一個更徹底的做法:

Labels as Values

你可以把跳轉目標隨意存儲,用字元串switch也輕而易舉


因為switch..case語句設計就是專門用於離散分支.


看錯題目,編輯


不是可以用...嗎?

我也是看別人代碼才發現的


讓我想起了c++ 11之後的auto關鍵字。。用上癮了就停不下來(因為真的方便吶)。。但是很多時候一換編譯器就傻逼了。。特別是對c++11支持不好的那些。。


推薦閱讀:

C語言里如何按需生成函數指針數組?
強制類型轉換表達式,結果是左值嗎?
sizeof(void*)的大小到底由何決定?
C++new運算符調用operator new 分配內存後, 構造函數調用及指針類型的轉換在哪裡完成?
有哪些較好的 C 語言程序源碼可供新手臨摹參考?

TAG:C編程語言 |