variable i behavioiur in switch case

223 Views Asked by At
#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.

  1. How it works?
  2. why it is?.

Thanks in advance.

3

There are 3 best solutions below

0
On

No statements before case: is executed inside switch except for variable declaration

#include<stdio.h>
int main() 
{
    int i=9;
    switch(i)
    {
         static int i=1;
         printf("I AM HERE %d\n", i);
         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);
}

The output would not print the string I AM HERE.

This switch is similar to writing as

    switch(i)
    {
         static int i=1;
         case 3:
                 i=i+11;
         case 4:
                 i=i+22;
         case 5:
                 i=i+33;
         default:
                 i=i+44;
          printf("%d",i);
    }

Since the value of i = 9, the default case is executed, which prints i = 45.

Also the scope of the variable i declared inside switch is limited only inside switch {} block. So once the control comes out of this scope the value of i = 9 which is declared in the block of main

0
On

You have two is... one visible only in the block that makes up the switch() body, and the other outside it. The switch(i) switches on the outer i (the one initialized to 9), so it goes to the default: case. When it then runs the following code under default::

     default:
             i=i+44;
      printf("%d",i);

...it is using the i declared inside the switch() block (the one initialized to 1), so it prints "45". This doesn't affect the i outside the switch, though.

After the switch() block's closing brace, the second printf():

   }
   printf("%d",i);

uses the i from outside the switch (the one initialized to 9), so it prints "9".

Essentially, the switch() body is a block, and a variable declared in a block is local to that block -- but can hide another variable with the same name from an outer scope.

0
On

The switch statement works as follows:

  1. The switch statement evaluates the expression in parentheses (in this case, i with a value of 9) and jumps to the appropriate case (which in this case is default, since no case 9 exists).
  2. Variables before the first case are declared. In this case, a new variable named i is declared with the value 1. Note that while this shares name with the previously declared i, they are separate variables and the new variable "overrides" the previous until the end of the switch statement. Since we jump directly to the default label, none of the code before this label is run.
  3. The code runs until either it encounters a break or the end of the switch statement. Then all variables declared inside the switch statement disappear, and in our case, the old i variable is again accessible.

At the default label, the program adds 44 to the "new" i variable (which previously was 1) and adds 44 to it, giving it the new value 45. It then prints the value.

After the switch statement, however, the new i variable has been unloaded and the old i variable becomes accessible again. It still has the old value of 9, so that is the value that is printed.