0

If I have the default case ahead of all other test cases in C, can that have any nefarious effects on the program?

For example,

switch(test)
{
    default:
        printf("hello");
        break;

    case 1:
        printf("1");
        break;
}

2 Answers2

2

It is valid.

A case and default label are equivalent to a goto label. See 6.8.1 Labeled statements.

Any statement may be preceded by a prefix that declares an identifier as a label name.

Labels in themselves do not alter the flow of control, which continues unimpeded across them.

The case statements and the default statement can occur in any order in the switch statement. The default is an optional clause that is matched if none of the constants in the case statements can be matched.

Community
  • 1
  • 1
Sadique
  • 22,572
  • 7
  • 65
  • 91
2

As long as you remember to put the break at the end of it, you're good.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101