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

時間 2021-06-03 12:17:26

1樓:

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

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

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

2樓:QKMICS

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

3樓:熊起

如果用非標擴充套件,有乙個更徹底的做法:

Labels as Values

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

4樓:

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

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

5樓:薄荷

case 4:

case 5:

case 6:

//Todo

break;

default:

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

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

6樓:

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:

C 語言中 int main 和 void main 有何區別?

DR.JC int main代表你main函式定義為整形,函式最後要加上返回值。void main或者就main代表你是無型別函式,函式最後不能加上返回值。 Eason同學 害,我一開始也一直寫void 為了省事 後來發現到了一些嚴謹的編譯器會直接報錯 貌似c必須要有返回值?反正後來我也return...

請問 C 語言中 和 的用法?

記住 有3個用途 1.乘號 Multiply 2 3 就是6 2.宣告指標 Pointer Statement int a 5 int ptr a 就是宣告變數a是5,把a的位址附到指標ptr上 3.解引用 Dereference ptr 單獨拿出來就是找出 ptr指標指向的值,按照第二點的說法就是...

在C語言中什麼意思?

男兒本色 在C語言中代表的是取位址符。scanf d a 這代表的是像a這個變數中輸入乙個整型數,我們知道,計算機的資料是放在電腦的儲存單元中的,在變數被建立的時候,在儲存器中就被分配了一定的儲存空間,這時我們在給這個變數賦值的時候就是往這個儲存空間中放數值,也就是直接將數值寫到這個空間中,a就代表...