Switch statement not doing what I expect

后端 未结 4 2129
悲&欢浪女
悲&欢浪女 2020-12-12 07:50

What is wrong with this code:

switch (n)
{
    case 0:   strcpy(resultString, \"Zero\");
    case 1:   strcpy(resultString, \"One\");
    case 2:   strcpy(re         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 08:17

    You need to break after each case.

    case 0: 
    
    do soemthing;
    
     break;
    
    case 1:
    
     do something;
    
     break;
    

    In many managed languages, it won't let "one case fall through to another," and throws an error. But C loves to let you do whatever you want!

提交回复
热议问题