如何返回main而不是调用它的函数

0 c function control-flow setjmp

我有一个function()电话anotherFunction().在里面anotherFunction(),有一个if语句,当满足时返回if并且不返回main().你怎么做到这一点?谢谢.

phu*_*clv 5

你不能在"标准"C中做到这一点.你可以用setjmplongjmp来实现它,但强烈建议不要这样做.

为什么不直接返回值anotherFuntion()并根据该值返回?像这样的东西

int anotherFunction()
{
    // ...
    if (some_condition)
        return 1; // return to main
    else
        return 0; // continue executing function()
}

void function()
{
    // ...
    int r = anotherFuntion();
    if (r)
        return;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

_Bool如果函数已被用于返回其他内容,则可以返回或返回指针

  • setjmp和longjmp都是标准C,因为你甚至引用了它们. (2认同)