C Non-local Jumps

Beginner
7 min read
Updated: Nov 2026
1 example
<setjmp.h>

What Is a Non-local Jump in C?

A non-local jump lets a program leave deep call frames and resume at an earlier setjmp() point. The <setjmp.h> header provides that pattern with setjmp(), longjmp(), and the jmp_buf type.

Think of setjmp() as saving a bookmark and longjmp() as jumping back to it. The jump skips normal returns, so you must keep the rules simple and clean up carefully.

Key idea.

Learn the save-and-jump pattern, then prefer ordinary return codes for everyday errors.

The Three Parts

PartRole
jmp_bufStorage for the saved calling environment.
setjmp()Saves the bookmark and returns 0 on the first pass.
longjmp()Restores the bookmark and makes setjmp() return again.

The Save and Jump Pattern

  1. Declare a jmp_buf. This holds the saved environment.
  2. Call setjmp(). On the first pass it returns 0.
  3. Do normal work. If something must abort early, call longjmp().
  4. Handle the jump path. When setjmp() returns nonzero, run recovery code.

Starter: Save, Jump, and Resume

C
#include <stdio.h>
#include <setjmp.h>

int main(void) {
    jmp_buf env;
    int code = setjmp(env);

    if (code == 0) {
        printf("first pass\n");
        longjmp(env, 3);
    }

    printf("resumed with %d\n", code);
    return 0;
}

How It Works

  1. Save the environment. setjmp(env) returns 0 the first time.
  2. Take the first branch. The program prints first pass.
  3. Jump with longjmp(). Control returns through setjmp() again.
  4. Read the new value. code becomes 3 and the resume line prints.

Open the Function Guides

Frequently Asked Questions

It is a jump back to an earlier active setjmp() call using longjmp(), instead of returning through every function one by one.
jmp_buf is the type that stores the calling environment saved by setjmp().
setjmp() returns 0 the first time. After longjmp(), it returns a nonzero value.
Learn setjmp() to save a bookmark, then longjmp() to jump back.
Usually no. Prefer return codes. Use setjmp/longjmp only when a special recovery path is worth the extra rules.

Browse setjmp.h functions

Open setjmp() and longjmp() with full examples.

Function index →

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.

8 people found this page helpful