This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C Language question

Hello,
I was working on a ARM project with my Keil toolchain, and by chance I just found that the following code is working (see case 2); I tested it in the ARM simulator using different values for alfa and beta.
Someone knows where can I find more detailed explanation about this behaviour of C language (K&R say nothing about "case" into "if"...)?
Thanks in advance.
Bruno

char alfa;
char beta;
char zeta;

int main (void)
{
while (1)
   {
   switch (alfa)
      {
      case 0:
         zeta = 17;
         break;

      case 1:
         zeta = 91;
         break;

      case 2:
         if (beta == 1)
            {

            case 3:
               zeta = 38;
               break;

            case 4:
               zeta = 47;
            }
         break;
      }
   }
}

Parents
  • What do you mean by "working"?

    "K&R say nothing about 'case' into 'if'..."

    Well, they couldn't possibly consider every permutation and combination of the language elements, could they... ;-)

    You'd need to think about the scope of case labels...

    You'd also need to comment this code very carefully so that you and other readers in the future know exactly what you intended by it...

    Or you could just re-write it in a more conventional manner so that there's no doubt...

Reply
  • What do you mean by "working"?

    "K&R say nothing about 'case' into 'if'..."

    Well, they couldn't possibly consider every permutation and combination of the language elements, could they... ;-)

    You'd need to think about the scope of case labels...

    You'd also need to comment this code very carefully so that you and other readers in the future know exactly what you intended by it...

    Or you could just re-write it in a more conventional manner so that there's no doubt...

Children
No data