2014-09-20 28 views
2
#include<stdio.h> 
int main() 
{ 
    switch(2) 
    { 
      case 1: 
        if(1) 
        { 
          case 2: 
            printf("hello\n"); 
        }; 
    } 
    return 0; 
} 

OUTPUT内也执行其壳体=你好 如我传递2switch case 1不真,那么也进入它并执行内部case 2代码。 它怎么进来case 1? 谢谢。开关盒是不正确的比是失败的一个

+0

FWIW,'switch(2)'也没有多大意义。您通常打开变量的值。 AFAICT,它不会输入'case 1:',它直接跳转到'case 2:'。 “如果(1)'也是无用的。 – 2014-09-20 15:22:47

+0

看看这里http://stackoverflow.com/questions/5569416/how-can-duffs-device-code-be-compiled – 2014-09-20 18:29:25

回答

2

switch(2)之后,它将立即跳转到case 2标签。它在case 1中包含的if块内的事实是无关紧要的。 case 2:的有效功能与goto标签的功能没有区别,所以无论它在哪里,它都会跳转到该标签。情况1不知怎么被输入。

为了澄清,适当的缩进它看起来如此:

#include<stdio.h> 
int main() { 
    switch(2) { 
    case 1: 
    if(1) { 
    case 2: 
     printf("hello\n"); 
    } 
    ; 
    } 
    return 0; 
} 
0

怎么就进入箱1?

它不输入case 1。其实if(1)在这里没有用处。上面的代码等同于

#include<stdio.h> 
int main() 
{ 
    switch(2) 
    { 
     case 1: 
     case 2: printf("hello\n"); 
    } 
    return 0; 
} 

要看到无关使用if可以取代if(1)if(0),你会发现,结果将是相同的。

+0

通过它的o/pi也知道它的类似2你说什么但是,我的问题是怎么样 ? – ayuj 2014-10-30 10:58:28