用C切换机箱

时间:2012-01-21 21:54:42

标签: gcc

有人可以帮助解决以下问题

#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");}
    }
}

结果是

 ./a.out 

switch 01
Ro
switch 02

switch default

我不知道这里发生了什么。

2 个答案:

答案 0 :(得分: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");}
}

应该是

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;
}

答案 1 :(得分:5)