Chapter 6. Nonlocal Jumps <setjmp.h>

Table of Contents
6.1. Save Calling Environment
6.2. Restore Calling Environment

The header <setjmp.h> defines the macro setjmp, and declares one function and one type, for bypassing the normal function call and return discipline.

The type declared is

jmp_buf

which is an array type suitable for holding the information needed to restore a calling environment.

6.1. Save Calling Environment

6.1.1. The setjmp macro

Synopsis
#include <setjmp.h>

int setjmp (jmp_buf env );

Description

The setjmp macro saves its calling environment in its jmp_buf argument for later use by the longjmp function.

Returns

If the return is from a direct invocation, the setjmp macro returns the value zero. If the return is from a call to longjmp function, the setjmp macro returns a nonzero value.

Environmental Constraint

An invocation of the setjmp macro shall appear in one of the following contexts:

  • the entire controlling expression of a selection or iteration statement:

  • one operand of a relational or equality operator with the other operand an integral constant expression, with the resulting expression being the entire controlling expression of a selection or iteration statement:

  • the operand of a unary ! operator with the resulting expression being the entire controlling expression of a selection or iteration statement: or

  • the entire expression of an expression statement (possibly cast to void).

Implementation Notes

If the program is running with expanded memory, then the address state is also saved in the jmp_buf argument.