C setjmp.h Functions

Definition
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.
Quick choice
Choose a Function by Job
| I want to... | Start with |
|---|---|
| Save a bookmark in the call stack | setjmp() |
| Jump back to that bookmark | longjmp() |
| Pass a status code through the jump | longjmp(env, val) |
| Tell first call from jump-back | setjmp() return value |
Index
C setjmp.h Functions Index
Remember
Key Takeaways
- Save before you jump. Call
setjmp()first, thenlongjmp()only while that function is still active. - Return values matter. First call is 0. After a jump,
setjmp()returns the value fromlongjmp()(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.
8 people found this page helpful
