How does switch statement work?
This is best explained by quotations from the c standard. I am quoting the relevant parts from the standard which apply to your question here.
6.8.4.2 The switch statement
Para 4:
A
switch
statement causes control to jump to, into, or past the statement that is theswitch
body, depending on the value of a controlling expression, and on the presence of adefault
label and the values of anycase
labels on or in the switch body......
Para 2:
If a
switch
statement has an associatedcase
ordefault
label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.154)
FootNote:
154) That is, the declaration either precedes the switch statement, or it follows the last
case
ordefault
label associated with theswitch
that is in the block containing the declaration.
Para 7:
EXAMPLE In the artificial program fragment
switch (expr)
{
int i = 4;
f(i);
case 0:
i = 17;
/* falls through into default code */
default:
printf("%d\n", i);
}
the object whose identifier is
i
exists with automatic storage duration (within the block) but is never initialized, and thus if the controlling expression has a nonzero value, the call to theprintf
function will access an indeterminate value. Similarly, the call to the functionf
cannot be reached.
The above mentioned applies to both of the code examples in the Question.Example 1
, i
has an Indeterminate value since it was never initialized & hence prints garbage, While inExample 2
, printf
call is not reached because the control jumps to the matching case
label.
Never write statements in switch which are not part of any case or default because they won't be executed.
NOTE: declaration can be written there but not statement (int i; is declaration but int i = 10; is declaration + assignment = statement so assignment will not be perform there..!)
switch(a)
{
printf("This will never print"); // this will never executed
case 1:
printf(" 1");
break;
default :
break;
}
Basically, a switch acts like a goto to the appropriate label -- intervening statements aren't executed. Variable definitions (which actually happen at compile time) do happen, but if they contain initialization, that's skipped too.