2013-04-27 50 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;没有)。

相关问题