Default Statement in C

By | February 21, 2023

Prerequisite – Control Statements in C
In C, the switch statement can have a default case, which is executed if none of the other cases match the value of the switch expression. The default case is optional, but it’s often used to provide a fallback option in case none of the other cases are appropriate.

Syntax:
The syntax for using the default case in a switch statement is as follows:

switch (expression) {
    case value1:
        // code to execute if expression == value1
        break;
    case value2:
        // code to execute if expression == value2
        break;
    // additional case statements...
    default:
        // code to execute if none of the other cases match
        break;
}

Examples:
Here’s examples that demonstrates the use of the default case in a switch statement:

Example-1:

#include <stdio.h>

int main() {
    int day = 8;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        case 6:
            printf("Saturday\n");
            break;
        case 7:
            printf("Sunday\n");
            break;
        default:
            printf("Invalid day\n");
            break;
    }

    return 0;
}

Output:

Invalid day

Example-2:

#include <stdio.h>

int main() {
    int age = 15;

    switch (age) {
        case 18:
            printf("You are eligible to vote!\n");
            break;
        case 21:
            printf("You are eligible to drink alcohol!\n");
            break;
        default:
            printf("You are not eligible for any special privileges.\n");
            break;
    }

    return 0;
}

In this example, the program uses a switch statement to check the value of the age variable. If age is equal to 18, the program prints a message saying that the person is eligible to vote. If age is equal to 21, the program prints a message saying that the person is eligible to drink alcohol. If age is any other value, the default statement is executed, and the program prints a message saying that the person is not eligible for any special privileges.

Output:

You are not eligible for any special privileges.

In this case, the value of age is 15, which doesn’t match any of the case statements. As a result, the default case is executed, and the program prints “You are not eligible for any special privileges.”

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.