Variable declaration inside a switch statement.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Srinu

    #1

    Variable declaration inside a switch statement.

    Hi all,

    If we compile the below piece of code, it gets compiled. But gives
    weird result.

    switch(x)
    {
    int y=2;

    case 1:
    printf("%d", y);
    }


    What, if any, the C standard says about it?

    Srinu.

  • abhy

    #2
    Re: Variable declaration inside a switch statement.

    On Oct 21, 4:00 pm, Srinu <sinu.nayak2... @gmail.comwrote :
    Hi all,
    >
    If we compile the below piece of code, it gets compiled. But gives
    weird result.
    >
    switch(x)
    {
    int y=2;
    >
    case 1:
    printf("%d", y);
    >
    }
    >
    What, if any, the C standard says about it?
    >
    Srinu.
    Can u tell me what weird results it gives ?

    Comment

    • Keith Thompson

      #3
      Re: Variable declaration inside a switch statement.

      abhy <abhijitkrao283 @gmail.comwrite s:
      On Oct 21, 4:00 pm, Srinu <sinu.nayak2... @gmail.comwrote :
      >If we compile the below piece of code, it gets compiled. But gives
      >weird result.
      >>
      >switch(x)
      >{
      >int y=2;
      >>
      >case 1:
      >printf("%d", y);
      >>
      >}
      >>
      >What, if any, the C standard says about it?
      >
      Can u tell me what weird results it gives ?
      Please don't use silly abbreviations like "u" for "you". This isn't a
      chat room. Take the time to spell out simple words.

      Yes, showing the actual questionable output is almost always a good
      idea. In this particular case, though, it's unnecessary. The
      variable y is uninitialized when it's printed; any output is possible.
      (Actually, the behavior is undefined, so in principle *anything* is
      possible, but it will most likely print some arbitrary value of type
      int.)

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • karthikbalaguru

        #4
        Re: Variable declaration inside a switch statement.

        On Oct 21, 4:00 pm, Srinu <sinu.nayak2... @gmail.comwrote :
        Hi all,
        >
        If we compile the below piece of code, it gets compiled. But gives
        weird result.
        >
        switch(x)
        {
        int y=2;
        >
        case 1:
        printf("%d", y);
        >
        }
        >
        What, if any, the C standard says about it?
        >
        This is a famous question in interviews :):)

        A switch statement never enters at the top.
        So, your initialization never gets executed.

        Karthik Balaguru

        Comment

        • CBFalconer

          #5
          Re: Variable declaration inside a switch statement.

          Srinu wrote:
          >
          If we compile the below piece of code, it gets compiled. But
          gives weird result.
          >
          switch(x) {
          int y=2;
          >
          case 1: printf("%d", y);
          }
          >
          What, if any, the C standard says about it?
          What you fail to realize is that initialization of an automatic
          variable requires the generation of code. That code has to go
          where the "int y = 2;" statement appears. There is no reason for
          the switch statement to transfer control to that code, so y is
          uninitialized (or worse) when the printf is executed.

          --
          Chuck F (cbfalconer at maineline dot net)
          Available for consulting/temporary embedded and systems.
          <http://cbfalconer.home .att.net>



          --
          Posted via a free Usenet account from http://www.teranews.com

          Comment

          • CBFalconer

            #6
            Re: Variable declaration inside a switch statement.

            abhy wrote:
            Srinu <sinu.nayak2... @gmail.comwrote :
            >
            .... snip ...
            >
            >What, if any, the C standard says about it?
            >
            Can u tell me what weird results it gives ?
            u hasn't posted in c.l.c for some time.

            --
            Chuck F (cbfalconer at maineline dot net)
            Available for consulting/temporary embedded and systems.
            <http://cbfalconer.home .att.net>


            --
            Posted via a free Usenet account from http://www.teranews.com

            Comment

            • abhy

              #7
              Re: Variable declaration inside a switch statement.

              On Oct 21, 10:43 pm, CBFalconer <cbfalco...@yah oo.comwrote:
              abhy wrote:
              Srinu <sinu.nayak2... @gmail.comwrote :
              >
              ... snip ...
              >
              What, if any, the C standard says about it?
              >
              Can u tell me what weird results it gives ?
              >
              u hasn't posted in c.l.c for some time.
              >
              --
              Chuck F (cbfalconer at maineline dot net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.net>
              >
              --
              Posted via a free Usenet account fromhttp://www.teranews.co m
              Hi Karthik

              I didn't get the meaning of your answer " A switch statement never
              enters at the top. "..?
              Please explain.

              Comment

              • Eric Sosman

                #8
                Re: Variable declaration inside a switch statement.

                abhy wrote On 10/22/07 16:45,:
                [...]
                I didn't get the meaning of your answer " A switch statement never
                enters at the top. "..?
                Please explain.
                He means this:

                switch (x) {
                /*
                * Nothing here will ever be executed,
                * because `switch' proceeds directly
                * to the chosen case or to the end of
                * the entire block if no case is chosen.
                */

                case 42:
                /*
                * This is the first piece of code that
                * the `switch' can ever execute.
                */
                ...
                }

                --
                Eric.Sosman@sun .com

                Comment

                • Srinu

                  #9
                  Re: Variable declaration inside a switch statement.

                  Dear friends,

                  Thanks a lot for your valuable suggestions.

                  Srinu.

                  Comment

                  Working...