2014-09-20 27 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