doubt in switch statement

gangadhar_kk's Avatar, Join Date: Nov 2006
Newbie Member
Hi all,
following is the cope snippet that i am trying to run.
Code:
  #include<stdio.h>
  int main()
  {
      int a=1;
      switch(a)
      {   int b=20;
          case 1: printf("b is %d\n",b);
                  break;
          default:printf("b is %d\n",b);
                  break;
      }
      return 0;
  }
i jus wanted to know whether the statements inside the switch statement but outside all the case blocks will execute or not.

Thanks in advance,
Gangadhar

Last edited by shabbir; 30Nov2006 at 10:30.. Reason: Code formating.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You have posted it as an Article under the Article / Source code section. I have moved it to the Queries and Discussion forum.

It should not and in MS compiler its an error.
0
gangadhar_kk's Avatar, Join Date: Nov 2006
Newbie Member
thanks shabbir, for moving my query to the right place..

but it wont give error on unix platform.
plz gimme some more info.

Thanks,
Gangadhar

Last edited by gangadhar_kk; 30Nov2006 at 10:48..
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
what output you are getting in printf.
0
gangadhar_kk's Avatar, Join Date: Nov 2006
Newbie Member
"b is 0"

In both the cases, its coming as zero only.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
That means its not executed. Dont you get any warning also for that.
0
gangadhar_kk's Avatar, Join Date: Nov 2006
Newbie Member
ya i got..
"warning : Statement can never be reached."

but still its printing no. If the statement is not reachable, it should give an error saying that the variable 'b' is not defined. And more over, how can it take it as zero?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
its some thing like writing some statement after the return statement. Its will also give something like code unreachable.

0 is the default value and so its not initialized but as the var is defined its showing as 0