Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Standard Library –

C Standard Library -

Introduction

setjmp.h The header file defines the macro setjmp(), the function longjmp() and the variable type jmp_buf, which bypasses the normal function call and return rules.

is a header file in the C Standard Library that provides a set of functions and macros for non-local jumps (i.e., jumping from one function to another function that was previously called, without using the normal function return mechanism). This mechanism is commonly used for error handling, exception handling, or for breaking out of deeply nested loops or recursion.

Library variables

The variables defined in the header file setjmp.h are listed below:

No. Variable & Description
1 **jmp_buf **

jmp_buf is a data type used to save the calling environment, including the stack pointer, instruction pointer, and registers. When executing setjmp() , this environment information is saved into jmp_buf a variable of that type.

Library macros

The following is the only macro defined in this library:

No. Macro & description
1 int setjmp(jmp_buf environment)
This macro saves the current environment in the variable environment so that the function longjmp() can use it later. If this macro returns directly from the macro call, it returns zero, but if it returns from a longjmp() function call, it returns a non-zero value.

Library functions

The following is the only function defined in the header file setjmp.h:

No. Function & Description
1 void longjmp(jmp_buf environment, int value)
This function restores the environment saved by the most recent call to setjmp() macro, and thejmp_buf parameters are set by a previous call to setjmp().

Example

The following example shows how to use setjmp() and longjmp() to implement a non-local jump:

Example

#include

#include

jmp_buf env

;

void

second

(

)

{

   

printf

(

"Entering second()\n"

)

;

   

longjmp

(

env

,

1

)

;

   

printf

(

"This line will never be executed\n"

)

;

}

void

first

(

)

{

   

printf

(

"Entering first()\n"

)

;

   

if

(

setjmp

(

env

)

==

0

)

{

       

printf

(

"Calling second() from first()\n"

)

;

        second

(

)

;

   

}

else

{

       

printf

(

"Returned to first() from second() using longjmp\n"

)

;

   

}

   

printf

(

"Exiting first()\n"

)

;

}

int

main

(

)

{

   

printf

(

"Starting main()\n"

)

;

    first

(

)

;

   

printf

(

"Exiting main()\n"

)

;

   

return

0

;

}

Running the above program will produce output similar to the following:

Starting main()

Entering first()

Calling second() from first()

Entering second()

Returned to first() from second() using longjmp

Exiting first()

Exiting main()

Code Analysis

Use Cases

Notes

Summary

provides setjmp() and longjmp() functions for implementing non-local jumps. These functions are very useful in error handling and exception handling, but must be used carefully to avoid potential problems.