2013-04-27 52 views
0
#include <stdio.h> 
#include <AssertMacros.h> 

int main(int argc, char* argv[]) 
{ 
    int error = 1; 

    verify_noerr(error); 
    require_noerr(error, Oops); //<---- Is Oops a callback method? 

    printf("You shouldn't be here!\n"); 

Oops: ;     // <--v____ Is this a method declaration? 
    return error;   // <--^  Why the ':' followed by the ';'? 
} 

此代碼是從iOS documentation from 2006。我意識到在C中,沒有聲明返回類型的方法的默認返回類型是int。但這真的是一種靠這個原理的方法嗎?以及爲什麼結腸分號?我最後的想法是它的C塊,但Wikipedia says otherwise是「糟糕:;返回錯誤;」 C中有效的方法聲明?

我很難過。

+3

這是一個goto標籤,並聲明它是有效的。 – 2013-04-27 22:01:43

+1

它與'switch'中的'case X:'基本相同。 – Sulthan 2013-04-27 22:06:31

回答

1

這在C編程中被稱爲標籤。

在C代碼,你可以使用goto跳轉到該標籤

goto Oops; 
2

此:

Oops: ; 

是一個標籤,它可以是一個goto的目標。

我猜require_noerr是一個宏,如果error是一個錯誤代碼,則該宏將擴展爲給定標籤的goto

發生錯誤時,您可以使用此係統退出功能。它允許在標籤和函數結尾之間進行清理代碼(簡單的if (error) return;沒有)。

相關問題