2

伙计们,

最近开始学习 C。
卡在一个点上。它关于 switch-case 语句的工作。

这是代码:

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

我个人认为,Default应该打印“”,因为它与任何大小写值都不匹配。
但是当我在 Turbo C 中运行代码时,我观察到的是:

Default
Case 0
Case 1
Case 2  

甚至在这里观察到同样的情况:http ://www.ideone.com/pFh1d

问题是什么 ?这是编译器问题还是我的代码中有任何错误?


编辑 :

PS:如果我必须先写默认情况,这有什么问题。有什么害处吗?

但是一旦编译器知道它必须执行 default 语句,为什么我们需要在 default case 语句之后放置一个 break 语句呢?

4

5 回答 5

7

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;
于 2011-04-25T07:41:26.287 回答
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:
}
于 2011-04-25T09:07:55.673 回答
1

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;
于 2011-04-25T07:34:08.040 回答
1

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.

于 2011-04-25T07:41:25.967 回答
1

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.

于 2016-06-02T12:31:42.233 回答