In C, longjmp() restores an earlier calling environment saved by setjmp(). It is declared in <setjmp.h> and is used for a non-local jump back to that saved point.
You pass a jmp_buf and an integer value. Control returns as if setjmp() had returned that value (or 1 if you pass 0). Use it only while the function that called setjmp() is still active.
Syntax
Pass the saved environment and the value that setjmp() should appear to return.
C
longjmp(env, 1);
Call
What it does
longjmp(env, 1);
Restores env and makes the matching setjmp() return 1.
Include <setjmp.h>. Always save the environment first with setjmp().
Library declarations
C
void longjmp(jmp_buf env, int val);
Parameters
Parameter
Description
env
Required. A jmp_buf previously filled by a successful setjmp() call that is still valid.
val
Required. The value setjmp() should return after the jump. If val is 0, setjmp() returns 1 instead.
Return Value
Return Value
Description
(none)
longjmp() does not return to its caller. Control resumes at the matching setjmp().
C Examples
Example 1 — Jump Back to setjmp() with a Value
Save a point with setjmp(), then call longjmp() so setjmp() returns again with a nonzero value.
C
#include <stdio.h>
#include <setjmp.h>
int main(void) {
jmp_buf env;
int code;
code = setjmp(env);
if (code == 0) {
printf("first pass\n");
longjmp(env, 2);
printf("never printed\n");
}
printf("back with code = %d\n", code);
return 0;
}
Output
first pass
back with code = 2
How It Works
Save the environment.setjmp(env) stores the current calling environment and returns 0 on the first pass.
Take the first branch. Because code is 0, the program prints first pass.
Jump with longjmp().longjmp(env, 2) restores env and never continues to never printed.
Resume at setjmp().setjmp() returns again with 2, so the program prints the final line.
Example 2 — Jump from a Nested Helper Function
Call longjmp() from a helper while setjmp() still lives in main.
C
#include <stdio.h>
#include <setjmp.h>
jmp_buf env;
void fail_fast(void) {
printf("helper jumps\n");
longjmp(env, 7);
}
int main(void) {
int code = setjmp(env);
if (code == 0) {
printf("calling helper\n");
fail_fast();
}
printf("main resumes with %d\n", code);
return 0;
}
Output
calling helper
helper jumps
main resumes with 7
How It Works
Save in main.setjmp(env) runs first and returns 0.
Enter the helper.fail_fast() prints a message, then calls longjmp().
Unwind to main. Control leaves the helper and resumes at setjmp() in main.
Read the value.code becomes 7, so main prints the resume message.
Example 3 — Passing 0 Still Returns 1 from setjmp()
If longjmp() is given 0, setjmp() returns 1 so the first pass and the jump can still be told apart.
C
#include <stdio.h>
#include <setjmp.h>
int main(void) {
jmp_buf env;
int code = setjmp(env);
if (code == 0) {
printf("about to jump with 0\n");
longjmp(env, 0);
}
printf("setjmp returned %d\n", code);
return 0;
}
Output
about to jump with 0
setjmp returned 1
How It Works
First return is 0. The direct setjmp() call returns 0.
Pass zero to longjmp().longjmp(env, 0) restores the environment with a zero request value.
Library substitutes 1. Because 0 would look like the first pass, setjmp() returns 1 instead.
Print the result. The program shows that the jump path is still distinguishable.
Frequently Asked Questions
longjmp() restores a jmp_buf saved by setjmp() and transfers control back to that setjmp() call.
No. longjmp() itself does not return. Execution continues as a return from the matching setjmp().
setjmp() returns 1 after the jump so the first call (0) and the jump path stay different.
No. The jmp_buf is valid only while the function that called setjmp() is still active.
Usually no. Prefer return codes and normal cleanup. longjmp() skips fclose, free, and similar cleanup unless you handle it yourself.