#include<stdio.h>
int main()
{
int i=9;
switch(i)
{
static int i=1;
i=3;
i=i*i;
case 3:
i=i+11;
case 4:
i=i+22;
case 5:
i=i+33;
default:
i=i+44;
printf("%d",i);
}
printf("%d",i);
}
I doesn't understand the actual behavioiur, the output is 45 9.
Could any one give me valid reason.
- How it works?
- why it is?.
Thanks in advance.
No statements before
case:
is executed inside switch except forvariable declaration
The output would not print the string
I AM HERE
.This switch is similar to writing as
Since the value of
i = 9
, thedefault
case is executed, which printsi = 45
.Also the scope of the variable
i
declared inside switch is limited only insideswitch {}
block. So once the control comes out of this scope the value ofi = 9
which is declared in the block ofmain