How does switch statement work?
How are statements that come before any case
labelled statement in a switch-case
block treated. Please explain the behavior of the following programs
prog1:
#include<stdio.h>
int main()
{
switch(1)
{
int i=0;
case 1:printf("%d",i);
}
getchar();
return 0;
}
Output: garbage value.
prog2:
#include<stdio.h>
int main()
{
switch(1)
{
printf("Inside Switch");
case 1:printf("Case 1n");
}
printf("Outside Switch");
getchar();
return 0;
}
Output:
Case 1
Outside Switch.
The statements before a case labelled statement seem unreachable according to program 2 but then why don't i get an error for an undeclared variable i
in the first program (only a warning).
Would be really helpful if someone could explain in detail that how the switch
statement is treated internally.
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 the switch
body, depending on the value of a controlling expression, and on the presence of a default
label and the values of any case
labels on or in the switch body......
Para 2:
If a switch
statement has an associated case
or default
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
or default
label associated with the switch
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("%dn", 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 the printf
function will access an indeterminate value. Similarly, the call to the function f
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 in
Example 2
, printf
call is not reached because the control jumps to the matching case
label.
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.
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;
}
please see this Why variables can not be declared in a switch statement just after labels?
链接地址: http://www.djcxy.com/p/84310.html上一篇: switch语句中的变量初始化问题
下一篇: switch语句如何工作?