Using enum type in a switch statement

cenumsswitch statement

I am using a switch statement to return from my main function early if some special case is detected. The special cases are encoded using an enum type, as shown below.

typedef enum {
    NEG_INF,
    ZERO,
    POS_INF,
    NOT_SPECIAL
} extrema;

int main(){

    // ...

    extrema check = POS_INF;

    switch(check){
        NEG_INF: printf("neg inf"); return 1;
        ZERO: printf("zero"); return 2;
        POS_INF: printf("pos inf"); return 3;
        default: printf("not special"); break;
    }

    // ...

    return 0;

}

Strangely enough, when I run this, the string not special is printed to the console and the rest of the main function carries on with execution.

How can I get the switch statement to function properly here? Thanks!

Best Answer

No case labels. You've got goto labels now. Try:

switch(check){
    case NEG_INF: printf("neg inf"); return 1;
    case ZERO: printf("zero"); return 2;
    case POS_INF: printf("pos inf"); return 3;
    default: printf("not special"); break;
}