C - Tricky Switch Case working .. !

别等时光非礼了梦想. 提交于 2019-12-11 02:44:15

问题


Folks,

Recently started learning C.
Stuck at a point. Its about working of switch-case statement.

Here's the code :

#include<stdio.h>

int main() {
        int i=4;
        switch(i) {

                default :       
                        printf("%s","Default");
                case 0:         
                        printf("%s","Case 0");
                case 1:         
                        printf("%s","Case 1");
                case 2:         
                        printf("%s","Case 2");

        return 0;
        }
 }  

I personally think, "Default" should be printed, as it doesn't match with any of case value.
But when I run the code in Turbo C, what I observed was this :

Default
Case 0
Case 1
Case 2  

Even same was observed here : http://www.ideone.com/pFh1d

What is the problem ? It is the compiler problem or any mistake in my code ?


EDIT :

PS : What wrong does it make if at all I have to write default case first. Any harm ?

But once the compiler knows that it has to execute the default statement, why we need to put a break statement after the statements of default case ?


回答1:


The switch statement will jump to the appropriate case or default and then the code will continue until the next break statement.

As your code has no break it will start off at the default: and simply continue through all the following statements. This can sometimes be a useful trick when programming similar conditions, but generally the lack of break statements will cause confusion.

Also the final return is in the wrong place it should be after the switch statement.

Amend as follows.

        int i=4;
        switch(i) {

                default :       
                        printf("%s","Default");
                        break;
                case 0:         
                        printf("%s","Case 0");
                        break;
                case 1:         
                        printf("%s","Case 1");
                        break;
                case 2:         
                        printf("%s","Case 2");
                        break;

        }
        return 0;



回答2:


But once the compiler knows that it has to execute the default statement, why we need to put a break statement after the statements of default case ?

Because that's the way the language works. The language's standard states that the execution should continue from the matching case-label. You might be wondering why the compiler does not add the break statements automatically, after each case. Well, if it did, we would not be able to do this:

switch(x){
case 1:
case 2:
case 3:
 // do something
 break;
case 4:
 // do something else
 break;
default:;
}

If the compiler added a break after each case, we'd have to rewrite the above code as follows:

  switch(x){
case 1:
 // do something
case 2:
 // do the same thing
case 3:
 // do the same thing
case 4:
 // do something else
default:
}



回答3:


It matched with defaule so Default and then due to absent of break; all the case executed

make it as follows to get your expected result

 default :       
                        printf("%s","Default"); break;
                case 0:         
                        printf("%s","Case 0");break;
                case 1:         
                        printf("%s","Case 1");break;
                case 2:         
                        printf("%s","Case 2");break;



回答4:


The body of switch statement in C language is one single continuous compound statement with several entry points. Each case/default label is an entry point. You can enter that compound statement at any entry point, and it will continue to execute all the way to the end (unless another jump statement intervenes, of course). In other words, case labels in switch work the same way goto labels do. The default label is not in any way different from case labels in this respect.

This is exactly what you observe in your experiment. The statement in your switch looks as follows

{
  printf("%s","Default");
  printf("%s","Case 0");
  printf("%s","Case 1");
  printf("%s","Case 2");
  return 0;
}

You enter this statement through your default: label at the very beginning. Since you do not alter the control flow after entering the body of your switch (aside from probably misplaced final return), all four printfs get a change to "fire".

The meaning of your question that begins with "But once the compiler knows that it has to execute the default statement..." is not clear to me. The functionality of switch/case construct in C is as I described above. That's what the "compiler knows". You, on the other hand, seem to follow some completely unfounded self-invented misconceptions about what switch/case does and for some reason expect the compiler to follow the same misconceptions. I'd say that you need to read a book on C language basics instead of trying to guess what various language elements do.

There's nothing wrong with making the default case first. Considering what I said above, in some situations the ordering of the labels inside the statement matters. When it matters, you have to arrange them in accordance with your intent. If you need your default case to be the first, make it first. If you don't need it (or it makes no difference) you can put it last or wherever else you want to put it.




回答5:


According to standards programmer has to place break manually after each case, even though it is a default case. If programmer is not place break means it will execute all the cases which is at and below to that case. Where standards call this type of switch programming as FALL THROUGH condition within the switch.



来源:https://stackoverflow.com/questions/5776253/c-tricky-switch-case-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!