setjmp.h
The setjmp.h
header provides the means to bypass the normal function call and return discipline through nonlocal jumps whilst preserving the calling environment. This may be used to implemented exception handling and cooperative multitasking (coroutines). By contrast, the keyword goto
performs local jumps.
Type aliases
jmp_buf
- Alias for an array
Contains information about the calling environment.
Macros
int setjmp(jmp_buf env);
- Expands to an expression
Saves the calling environment to itsenv
argument.
If the return is from a direct invocation, the macro returns 0. If the return is from a call to thelongjmp
function, the macro returns a nonzero value.
Functions
_Noreturn void longjmp(jmp_buf env, int val);
- Restores the calling environment saved by the most recent invocation of the
setjmp
macro with the correspondingjmp_buf
argument.val
is the value returned by thesetjmp
macro. If set to 0, then the function returns 1 in order to avoid undefined behaviour.