CKTIS / C
longjmp — C23 control-transfer function
`longjmp(env, val)` from `<setjmp.h>` restores the environment saved by the most recent `setjmp(env)` and does not return to its call site. The resumed `setjmp` returns `val`, or returns `1` when `val == 0`, so a jump remains distinguishable from the initial pass.
Version: C23
Syntax
[[noreturn]] void longjmp(jmp_buf env, int val);longjmp(env, val) from <setjmp.h> restores the environment saved by the most recent setjmp(env) and does not return to its call site. The resumed setjmp returns val, or returns 1 when val == 0, so a jump remains distinguishable from the initial pass.
The example stores a file-scope jmp_buf; after the initial zero result it calls a helper that executes longjmp(environment, 42). Output 42 proves an actual nonlocal transfer rather than simulating it with an ordinary return.
The function that executed setjmp must still be active, and the jump must remain in the same thread; otherwise behavior is undefined. Automatic objects local to that function, changed after setjmp and not volatile, have indeterminate values after the jump. Jumping to an environment saved within a scope of a variably modified object after leaving that scope is also UB. longjmp releases no resources and bypasses ordinary cleanup paths.
Examples
#include <setjmp.h>
#include <stdio.h>
static jmp_buf environment;
static void finish(void){longjmp(environment,42);}
int main(void){int value=setjmp(environment);if(value==0)finish();printf("%d\n",value);return 0;}
Result
42
The example stores a file-scope `jmp_buf`; after the initial zero result it calls a helper that executes `longjmp(environment, 42)`. Output `42` proves an actual nonlocal transfer rather than simulating it with an ordinary `return`.