Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   doubt in switch statement (http://www.go4expert.com/forums/doubt-switch-statement-t2052/)

gangadhar_kk 29Nov2006 10:53

doubt in switch statement
 
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

shabbir 30Nov2006 10:39

Re: doubt in switch statement
 
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.

gangadhar_kk 30Nov2006 10:45

Re: doubt in switch statement
 
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

shabbir 30Nov2006 10:53

Re: doubt in switch statement
 
what output you are getting in printf.

gangadhar_kk 30Nov2006 10:55

Re: doubt in switch statement
 
"b is 0"

In both the cases, its coming as zero only.

shabbir 30Nov2006 11:00

Re: doubt in switch statement
 
That means its not executed. Dont you get any warning also for that.

gangadhar_kk 30Nov2006 11:26

Re: doubt in switch statement
 
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?

shabbir 30Nov2006 11:44

Re: doubt in switch statement
 
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


All times are GMT +5.5. The time now is 23:23.