Supports transfer of control between functions.
The following macros are implementation-defined.
The setjmp function saves the execution environment of the current function. The location in the program that called the setjmp function can subsequently be returned to by calling the longjmp function.
An example of how transfer of control between functions is supported using the setjmp and longjmp functions is shown below.
1 #include <stdio.h>
2 #include <setjmp.h>
3 jmp_buf env;
4 void sub( );
5 void main( )
6 {
7
8 if (setjmp(env)!=0){
9 printf("return from longjmp\n");
10 exit(0);
11 }
12 sub( );
13 }
14
15 void sub( )
16 {
17 printf("subroutine is running \n");
18 longjmp(env, 1);
19 }
The setjmp function is called in line 8. At this time, the environment in which the setjmp function was called is saved in jmp_buf type variable env. The return value in this case is 0, and therefore function sub is called next.
The environment saved in variable env is restored by the longjmp function called within function sub. As a result, the program behaves just as if a return had been made from the setjmp function in line 8. However, the return value at this time is 1 specified by the second argument of the longjmp function. As a result, execution proceeds to line 9.
Saves the execution environment of the currently executing function in the specified storage area.
#include <setjmp.h>
long setjmp (jmp_buf env);
env Pointer to storage area in which execution environment is to be saved
When setjmp function is called: 0
On return from longjmp function: Nonzero
The execution environment saved by the setjmp function is used by the longjmp function. The return value is 0 when the function is called as the setjmp function, but the return value on return from the longjmp function is the value of the second parameter specified by the longjmp function.
If the setjmp function is called from a complex expression, part of the current execution environment, such as the intermediate result of expression evaluation, may be lost. The setjmp function should only be used in the form of a comparison between the result of the setjmp function and a constant expression, and should not be called within a complex expression.
Do not call the setjmp function indirectly using a pointer.
Restores the function execution environment saved by the setjmp function, and transfers control to the program location at which the setjmp function was called.
#include <setjmp.h>
void longjmp (jmp_buf env, long ret);
env Pointer to storage area in which execution environment was saved
ret Return code to setjmp function
From the storage area specified by the first parameter env, the longjmp function restores the function execution environment saved by the most recent invocation of the setjmp function in the same program, and transfers control to the program location at which that setjmp function was called. The value of the second parameter ret of the longjmp function is returned as the setjmp function return value. However, if ret is 0, the value 1 is returned to the setjmp function as a return value.
If the setjmp function has not been called, or if the function that called the setjmp function has already executed a return statement, the operation of the longjmp function is not guaranteed.