Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

setjmp — save a calling environment

`setjmp` saves an environment in `jmp_buf`, allowing execution to resume through `longjmp`.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <setjmp.h>
int setjmp(jmp_buf env);

setjmp from <setjmp.h> is described as a macro, not an ordinary function. It initially returns 0; after longjmp(env, value), it returns value, with 0 replaced by 1.

A setjmp invocation can be the entire controlling expression of a selection or iteration statement. It can also be the operand of unary ! or one operand of a relational or equality operator whose other operand is an integer constant expression. In those last two cases, the resulting expression must be the entire controlling expression of a selection or iteration statement. The invocation can also be the entire expression of an expression statement, optionally cast to void. Any other context causes undefined behavior. This includes int result = setjmp(env); and use as the controlling expression of a _Generic selection, even though that controlling expression is not evaluated.

The target must remain active in the same thread. Changed non-volatile automatic local objects in the saving function become indeterminate. Jumping does not undo I/O.

Examples

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

int main(void)
{
    jmp_buf env;
    if (setjmp(env) == 0) {
        longjmp(env, 1);
    } else {
        puts("resumed");
    }
    return 0;
}

Result

resumed

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources