Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example declares variables and contains executable statements before the first case label within the switch statement.

Code Block
bgColor#FFCCCC
langc
int func(int expr) {
  switch(expr){
    int i = 4;
    f(i);
  case 0:
    i = 17;
  /* falls through into default code */
  default:
    printf(“%d\n”, i);
  }
  return 0;
}

...

In this compliant solution, the statements before the first case label occur before the switch statement, improving the predictability and readability of the code.

Code Block
bgColor#ccccff
langc
int func(int expr) {
  int i = 4;    /* Move the code outside the switch block */
  f(i);         /* Now the statements will get executed */

  switch(expr) {
    case 0:
      i = 17;
      /*falls through into default code */
    default:
      printf(“%d\n”, i);
  }
  return 0;
}

...