温馨提示×

default在c语言中怎么使用

小亿
82
2024-05-27 14:40:08
栏目: 编程语言

在C语言中,可以使用switch语句来处理默认情况。switch语句用于根据表达式的值执行不同的操作。默认情况可以在switch语句中设置,用于处理未在其他case中匹配到的情况。

示例代码如下:

#include <stdio.h>

int main() {
    int num = 3;

    switch(num) {
        case 1:
            printf("Number is 1\n");
            break;
        case 2:
            printf("Number is 2\n");
            break;
        default:
            printf("Number is not 1 or 2\n");
            break;
    }

    return 0;
}

在上面的示例中,如果变量num的值为3,则会执行默认的情况,输出"Number is not 1 or 2"。

0