setjmp.h is a header defined in the C standard library to provide "non-local jumps": control flow that deviates from the usual subroutine call and return sequence.
A typical use of setjmp/longjmp is implementation of an exception mechanism that exploits the ability of longjmp to reestablish program or thread state, even across multiple levels of function calls.
This process can be imagined to be a "jump" back to the point of program execution where setjmp saved the environment.
[2] It notes that it can simply be a one-member-long array with its single member being the actual data; indeed, this is the approach employed by the GNU C library, which defines the type as struct __jmp_buf_tag[1].
The call to longjmp is analogous to a throw statement, allowing an exception to return an error status directly to the setjmp.
The following code adheres to the 1999 ISO C standard and Single UNIX Specification by invoking setjmp in a limited range of contexts:[6] Following these rules can make it easier for the implementation to create the environment buffer, which can be a sensitive operation.
[2] More general use of setjmp can cause undefined behaviour, such as corruption of local variables; conforming compilers and environments are not required to protect or even warn against such usage.
However, slightly more sophisticated idioms such as switch ((exception_type = setjmp(env))) { } are common in literature and practice, and remain relatively portable.