default在c語(yǔ)言中怎么使用

小億
95
2024-05-27 14:40:08

在C語(yǔ)言中,可以使用switch語(yǔ)句來(lái)處理默認(rèn)情況。switch語(yǔ)句用于根據(jù)表達(dá)式的值執(zhí)行不同的操作。默認(rèn)情況可以在switch語(yǔ)句中設(shè)置,用于處理未在其他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,則會(huì)執(zhí)行默認(rèn)的情況,輸出"Number is not 1 or 2"。

0