在switch语句中使用enum类型

时间:2022-11-30 07:43:49

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.

如果检测到某些特殊情况,我正在使用switch语句提前从我的main函数返回。特殊情况使用枚举类型进行编码,如下所示。

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!

如何让switch语句在这里正常运行?谢谢!

3 个解决方案

#1


18  

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

没有案例标签。你现在有了goto标签。尝试:

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;
}

#2


2  

You're missing the all-important case:

你错过了最重要的案例:

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;
}

You created some (unused) labels with the same names as your enumeration constants (which is why it compiled).

您创建了一些(未使用的)标签,其名称与枚举常量相同(这就是它编译的原因)。

#3


2  

You haven't used the keyword "case". The version given below will work fine.

您还没有使用关键字“case”。下面给出的版本可以正常工作。

typedef enum {
    NEG_INF,
    ZERO,
    POS_INF,
    NOT_SPECIAL

} extrema;

int main(){

    extrema check = POS_INF;

    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;
    }

    return 0;

}

#1


18  

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

没有案例标签。你现在有了goto标签。尝试:

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;
}

#2


2  

You're missing the all-important case:

你错过了最重要的案例:

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;
}

You created some (unused) labels with the same names as your enumeration constants (which is why it compiled).

您创建了一些(未使用的)标签,其名称与枚举常量相同(这就是它编译的原因)。

#3


2  

You haven't used the keyword "case". The version given below will work fine.

您还没有使用关键字“case”。下面给出的版本可以正常工作。

typedef enum {
    NEG_INF,
    ZERO,
    POS_INF,
    NOT_SPECIAL

} extrema;

int main(){

    extrema check = POS_INF;

    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;
    }

    return 0;

}