Default Keyword


In C programming, the 'default' keyword is used in conjunction with the 'switch' statement.

It specifies the block of code to be executed when none of the cases in the 'switch' statement match the evaluated expression.

Here's an example of using the 'default' keyword within a 'switch' statement:


int day = 5;
switch (day) {
  case 1:
    printf("Monday");
    break;
  case 2:
    printf("Tuesday");
    break;
  default:
    printf("Invalid day");
    break;
}

    

In this example, the 'switch' statement evaluates the value of the 'day' variable. If none of the cases match, the 'default' block is executed.

In this case, since 'day' has a value of 5, which does not match any of the cases, the statement "Invalid day" is printed.

The 'default' keyword is optional in a 'switch' statement.

It provides a fallback option when none of the cases match the evaluated expression.

Including a 'default' case ensures that there is always a code block to be executed in case no specific match is found.

Loading...

Search