Name

longjmp

Synopsis

Jump to a previously defined point in the program

#include <setjmp.h>
voidlongjmp( jmp_buf environment, int returnval );

The longjmp() function allows the program to jump to a point that was previously defined by calling the macro setjmp(). Unlike the goto statement, the longjmp() call does not need to be within the same function as its destination. The use of setjmp() and longjmp() can make a program harder to read and maintain, but they are useful as a way to escape from function calls in case of errors.

The environment argument contains the processor and stack environment corresponding to the destination, and must be obtained from a prior setjmp() call. Its type, jmp_buf, is defined in setjmp.h.

The longjmp() function does not return. Instead, the program continues as if returning from the setjmp() call, except that the returnval argument passed to longjmp() appears as the return value of setjmp(). This value allows the setjmp() caller to determine whether the initial setjmp() call has just returned, or whether a longjmp() call has occurred. setjmp() itself returns 0. If setjmp() appears to return any other value, then that point in the program was reached by calling longjmp(). If the returnval argument in the longjmp() call is 0, it is replaced with 1 as the apparent return value after the corresponding setjmp() call. The longjmp() call must not occur after the function that called setjmp() returns. Furthermore, if any variables with automatic storage duration ...

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.