Versions Compared

Key

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

...

In the example mentioned below, the variable i will be instantiated with automatic storage duration within the block, but it’s never initialized. Thus, if the controlling expression has a non-zero value, the cause to printf will access an indeterminate value of i. Similarly, the call to function will also never get executed.

Warning
titlefalse
Wiki Markup

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;
}

Compliant Solution

In the compliant solution, by moving the statements before the first case statement outside the switch block, the execution can be ensured and result in an expected behavior.