C Standard Libraries C++



void longjmp(jmp_buf qEnvironment, int qReturn);


This function sets the stack environment and execution locale via the passsed-in argument, qEnvironment. The stack environment is typically set via a prior call to setjmp(). The argument qReturn is the value that will be returned the next time that setjmp() is called. The function longjmp() can be used to jump execution back to a prior setjmp() call by passing in the retrieved stack environment variable.


#include <iostream>
#include <csetjmp>

int main() {
    using namespace std;

    int x = 0;
    jmp_buf qEnv;

    // Use setjmp() and longjmp() to create a loop
    int iRet = setjmp(qEnv);
    cout << "Return value   = " << iRet << endl;
    if (x < 10) {
        // This jumps back to setjmp()
        longjmp(qEnv, x);

    return 0;


longjmp() Output

© 2007–2023 XoaX.net LLC. All rights reserved.