doubt in switch statement

Discussion in 'C' started by gangadhar_kk, Nov 29, 2006.

  1. gangadhar_kk

    gangadhar_kk New Member

    Joined:
    Nov 29, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Nov 30, 2006
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. gangadhar_kk

    gangadhar_kk New Member

    Joined:
    Nov 29, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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: Nov 30, 2006
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    what output you are getting in printf.
     
  5. gangadhar_kk

    gangadhar_kk New Member

    Joined:
    Nov 29, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    "b is 0"

    In both the cases, its coming as zero only.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    That means its not executed. Dont you get any warning also for that.
     
  7. gangadhar_kk

    gangadhar_kk New Member

    Joined:
    Nov 29, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice