在Linux中用GCC时switch语句的另一种简洁写法

源代码:
#include <stdio.h>
int main(int argc, char **argv)
{
    int word = 0;
    while (word != 1111) {
        printf("Please input a integer:\n");
        scanf("%d", &word);
        printf("word = %d\n", word);
        switch (word) {
        case 0...20:            //注意格式是:0+空格+...+空格+20
            printf("The word(0-20) is %d\n", word);
            break;
        case 21...40:
            printf("The word(21-40) is %d\n", word);
            break;
        case 41...60:
            printf("The word(41-60) is %d\n", word);
            break;
        case 61...80:
            printf("The word(61-80) is %d\n", word);
            break;
        case 81...100:
            printf("The word(81-100) is %d\n", word);
            break;
        default:
            printf("The word > 100\n");
            break;
        }
    }
    return 0;
}
编译:gcc -Wall switchcase.c
运行:. /a.out
结果:
Please input a integer:
12
word = 12
The word(0-20) is 12
Please input a integer:
34
word = 34
The word(21-40) is 34
Please input a integer:
78
word = 78
The word(61-80) is 78
Please input a integer:
120
word = 120
The word > 100
Please input a integer:
1111
word = 1111
The word > 100

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsddp.html