C longjmp() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
setjmp.h

Definition

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);
CallWhat 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

ParameterDescription
envRequired. A jmp_buf previously filled by a successful setjmp() call that is still valid.
valRequired. The value setjmp() should return after the jump. If val is 0, setjmp() returns 1 instead.

Return Value

Return ValueDescription
(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;
}

How It Works

  1. Save the environment. setjmp(env) stores the current calling environment and returns 0 on the first pass.
  2. Take the first branch. Because code is 0, the program prints first pass.
  3. Jump with longjmp(). longjmp(env, 2) restores env and never continues to never printed.
  4. 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;
}

How It Works

  1. Save in main. setjmp(env) runs first and returns 0.
  2. Enter the helper. fail_fast() prints a message, then calls longjmp().
  3. Unwind to main. Control leaves the helper and resumes at setjmp() in main.
  4. 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;
}

How It Works

  1. First return is 0. The direct setjmp() call returns 0.
  2. Pass zero to longjmp(). longjmp(env, 0) restores the environment with a zero request value.
  3. Library substitutes 1. Because 0 would look like the first pass, setjmp() returns 1 instead.
  4. 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.

Next: setjmp() Function

Learn how to save the calling environment that longjmp() restores.

setjmp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful