6.12.2 longjmp Function
A function that restores the environment saved by
setjmp
.
Include
<setjmp.h>
Prototype
void longjmp(jmp_buf env, int val);
Arguments
env
- variable where environment is stored
val
- value to be returned to
setjmp
call
Remarks
The value parameter, val
, should be non-zero. If
longjmp
is invoked from a nested signal handler (that is, invoked
as a result of a signal raised during the handling of another signal), the behavior is
undefined.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf jb;
void inner (void)
{
longjmp(jb, 5);
}
int main (void)
{
int i;
if((i = setjmp(jb))) {
printf("setjmp returned %d\n" i);
exit(0);
}
printf("setjmp returned 0 - good\n");
printf("calling inner...\n");
inner();
printf("inner returned - bad!\n");
}
Example Output
setjmp returned 0 - good
calling inner...
setjmp returned 5