-3
\$\begingroup\$
int a=4;
int b=4;

int main()
{
    switch(a)
    {
        case 1:
            {
             case 4:
                  b=7;
             case 5:
                  b=8; //added newly to check in disassembly
            }
        break;
    }
    printf("%d",b);
    return 0;
}

In this code, the value of "b" printed is 8. Why does the code execute the inner case statement,though the external case condition is not satisfied? Does the switch behave directly as a goto label irrespective of where the case label has been placed?

Update: Disassembly code
    25:         switch(a) 
    26:         { 
    27:                         case 1: 
    28:                                         { 
0x00000200 481A      LDR      r0,[pc,#104]  ; @0x0000026C
0x00000202 6800      LDR      r0,[r0,#0x00]
0x00000204 2801      CMP      r0,#0x01
0x00000206 D004      BEQ      0x00000212
0x00000208 2804      CMP      r0,#0x04
0x0000020A D003      BEQ      0x00000214
0x0000020C 2805      CMP      r0,#0x05
0x0000020E D109      BNE      0x00000224
0x00000210 E004      B        0x0000021C
    29:                                          case 4: 
0x00000212 BF00      NOP      
    30:                                                                 b=7; 
0x00000214 2007      MOVS     r0,#0x07
0x00000216 4916      LDR      r1,[pc,#88]  ; @0x00000270
0x00000218 6008      STR      r0,[r1,#0x00]
    31:                                          case 5: 
0x0000021A BF00      NOP      
    32:                                                                 b=8; 
    33:  
    34:                                         } 
0x0000021C 2008      MOVS     r0,#0x08
0x0000021E 4914      LDR      r1,[pc,#80]  ; @0x00000270
0x00000220 6008      STR      r0,[r1,#0x00]
    35:                         break; 
    36:         } 
    37:          
\$\endgroup\$
26
  • 5
    \$\begingroup\$ I'm voting to close this question as off-topic because it is about C programming in general and not specific to embedded systems. \$\endgroup\$
    – The Photon
    Feb 9, 2016 at 4:34
  • 1
    \$\begingroup\$ Should probably be migrated to StackOverflow. It's not a terrible question, just not the right SE for it. \$\endgroup\$ Feb 9, 2016 at 4:35
  • 1
    \$\begingroup\$ For example, Why was the switch statement designed to need a break? \$\endgroup\$
    – The Photon
    Feb 9, 2016 at 4:39
  • 1
    \$\begingroup\$ Or How does falling through cases in c work?. \$\endgroup\$
    – The Photon
    Feb 9, 2016 at 4:42
  • 2
    \$\begingroup\$ To be fair, the on-topic help for this site is crappy. It says that "the writing of firmware for bare-metal or RTOS applications" is on-topic, but "Programming software for a PC" is off-topic. Pure, generic programming questions are neither. According to this meta, it is up to the OP to decide where to post. \$\endgroup\$
    – Lundin
    Feb 9, 2016 at 9:28

1 Answer 1

4
\$\begingroup\$

There isn't really such a thing as a nested case within a case in c, and it is not an nested switch statement (there is no switch()!). What you are looking at is really badly formatted code and poor use of blocks.

Essentially the code is this:

int main()
{
    switch(a)
    {
        case 1:
        case 4:
            b=7;
            break;
    }
    printf("%d",b);
    return 0;
}

The case of 1 falls through into the case of 4 - so basically both 1 and 4 will set b equal to 7.


In C, you can use the {} to define a scope. So if statements, functions, and so forth. You can also use them in the middle of code to define variables scoped only to that region, e.g.

int main() {

    ...
    {
        int a;
        //do something with a
    }
    //a goes out of scope here.
    ...
}

In fact you can do the same with case statements if for example you need a local variable:

int main()
{
    switch(a)
    {
        case 1:
        case 4: {
            int c = 1;
            //Do something with c
            b=b+c;
            break;
        } //c goes out of scope here
    }
    printf("%d",b);
    return 0;
}

Adding the scope around the whole case as it stands is like in the above example. But the way it is added to the sample in the question is simply obscure - will have the same effect as in my example above, but I've only ever seen the opening { it after the case ... :, not before.

\$\endgroup\$
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.