C & C++

C & C++

Made by DeepSource

Missing default case in switch statement CXX-W1164

Anti-pattern
Minor

Default case provides switch statements with fallback, and in general is a good to have. Hence consider adding default case to switch.

Bad practice

int x = 10;
switch x {
    case 1: {
        printf("You chose 1.");
        break;
    }
    case 2: {
        printf("You chose 2.");
        break;
    }
}

Recommended

int x = 10;
switch x {
    case 1: {
        printf("You chose 1.");
        break;
    }
    case 2: {
        printf("You chose 2.");
        break;
    }
    default: {
        printf("Invalid choice!");
        break;
    }
}