Choosing enum element using “…” [duplicate]

回眸只為那壹抹淺笑 提交于 2020-06-28 09:25:15

问题


Is '...' symbol is a c language keyword?

The code:

#include <stdio.h>

typedef enum {
    A=0,B,C,D,E,F,G,H,I,J,K,M
} alpha;


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


    alpha table = C;

    switch (table)
    {
        case A ... D:
        /* I have never seen "..." grammar in textbook */
            printf("Oh my god\n");
            break;
        default:
            printf("default\n");
            break;
    }
    return 0;
}

Is ... allowed in C for range?


回答1:


It's not standard C, but a GCC extension:

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.

More in GCC extension: Case ranges




回答2:


This is called Case Ranges. And no, this is not a standard C feature.

It is implemented as gcc extension. This is just another way to use fall-through case statement.



来源:https://stackoverflow.com/questions/37318196/choosing-enum-element-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!