C setjmp.h Functions

Beginner
6 min read
Updated: Nov 2026
2 tutorials
<setjmp.h>

Definition and Usage

<setjmp.h> is the C header for non-local jumps. Its two functions are setjmp(), which saves a calling environment in a jmp_buf, and longjmp(), which restores that environment and resumes there.

This page is the catalog for both tutorials. Start from the beginner overview if the idea is new, then open each function guide for syntax, return values, and three examples.

Need the model first?

Read C Non-local Jumps for jmp_buf, the first-return vs jump-back pattern, and safety rules.

Choose a Function by Job

I want to...Start with
Save a bookmark in the call stacksetjmp()
Jump back to that bookmarklongjmp()
Pass a status code through the jumplongjmp(env, val)
Tell first call from jump-backsetjmp() return value

C setjmp.h Functions Index

FunctionDescriptionTutorial
longjmp()Restore a saved jmp_buf and resume at setjmp().Open
setjmp()Save the calling environment in a jmp_buf.Open

Key Takeaways

  • Save before you jump. Call setjmp() first, then longjmp() only while that function is still active.
  • Return values matter. First call is 0. After a jump, setjmp() returns the value from longjmp() (or 1 if that value was 0).
  • Prefer normal returns. Everyday errors are clearer with return codes and cleanup than with non-local jumps.

Frequently Asked Questions

It declares setjmp() and longjmp(), plus the jmp_buf type used to save a calling environment.
Start with setjmp() to save a bookmark, then longjmp() to jump back to it.
setjmp() returns 0 on the first call. After longjmp(env, val), setjmp() returns val, or 1 if val was 0.
Only while the function that called setjmp() is still active. Do not jump into a finished call.
Usually no. Prefer return codes and normal cleanup. Use setjmp/longjmp only for special recovery paths.

Start with setjmp()

Learn how to save the calling environment first.

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.

8 people found this page helpful