< c‎ | program
Revision as of 04:13, 25 April 2014 by D41D8CD98F (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Defined in header <setjmp.h>
void longjmp( jmp_buf env, int status );

Loads the execution context env saved by a previous call to setjmp. This function does not return. Control is transferred to the call site of the macro setjmp that set up env. That setjmp then returns the value, passed as the status.

If the function that called setjmp has exited, the behavior is undefined (in other words, only long jumps up the call stack are allowed).


[edit] Parameters

env - variable referring to the execution state of the program saved by setjmp
status - the value to return from setjmp. If it is equal to 0, 1 is used instead

[edit] Return value


[edit] Example

Typical usage: exception mechanism

#include <stdio.h>
#include <setjmp.h>
static jmp_buf call_env;
void f_2(void)
    printf("Enter f_2()\n");
    /* Restore calling environment:                             */
    /*   - "return" to the program state when setjmp was called */
    /*   - setjmp will return second arg of longjmp (1)         */
    longjmp(call_env,1);   /* declare an exception with code 1 */
void f_1(void)
    printf("Enter f_1()\n");
    printf("In f_1()\n");   /* never executed */
int main(void)
    int rtn;
    if (!(rtn=setjmp(call_env)))  /* save calling environment; return 0 */
    {  /* normal processing here */
       printf("In main(): %d\n", rtn);   /* 0 */
    else   /* exception mechanism here */
       /* when longjmp jumps back, setjmp returns second arg of longjmp */
       printf("In main(): %d\n", rtn);   /* 1 */
    return 0;


In main(): 0
Enter f_1()
Enter f_2()
In main(): 1

[edit] See also

saves the context
(function macro) [edit]
C++ documentation for longjmp