Default Statement in C

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 21 Feb, 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read