0

Can someone help with the below

#include <stdio.h>

main ()
{
    char receive_buff [] ={0x01,0x00,0x01,0x01,0x00,0x00};

    switch( receive_buff[0] ) 
    {
        case 0x00:
            {printf("\nswitch 00\n");}
        case 0x01:
            {printf("\nswitch 01\n");}
        case 0x02:
            {printf("\nswitch 02\n");}
        default :
            {printf("\nswitch default\n");}
    }
}

the result is

 ./a.out 

switch 01
Ro
switch 02

switch default

I don't know what going on here.

2
  • main () should be int main(void) Commented Jan 21, 2012 at 23:09
  • Could You explain what is Ro? Commented May 6, 2013 at 14:17

2 Answers 2

6
switch( receive_buff[0] ) 
{
    case 0x00:
        {printf("\nswitch 00\n");}
    case 0x01:
        {printf("\nswitch 01\n");}
    case 0x02:
        {printf("\nswitch 02\n");}

    default :
        {printf("\nswitch defualt\n");}
}

Should be

switch( receive_buff[0] ) 
{
    case 0x00:
        {printf("\nswitch 00\n");}
        break;
    case 0x01:
        {printf("\nswitch 01\n");}
        break;
    case 0x02:
        {printf("\nswitch 02\n");}
        break;
    default :
        {printf("\nswitch defualt\n");}
        break;
}
Sign up to request clarification or add additional context in comments.

Comments

5

You need a break statement after each set of actions, or the C switch will fall through. See http://en.wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_D.2C_Java.2C_PHP.2C_ActionScript.2C_JavaScript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.