C longjmp function causes program to jump to a preset point set setjmp()
Syntax
C longjmp function has the following syntax.
void longjmp(jmp_buf envbuf, int status);
Header
C longjmp function is from file setjmp.h
.
Description
The longjmp()
function causes program execution
to resume at the point of the last call to setjmp()
.
longjmp()
and setjmp()
provide a means of jumping between functions.
The common use of longjmp() is to return from a deeply nested set of routines in case of error.
Example
Jump to a preset point set by setjmp() by using C longjmp function.
#include <setjmp.h>
#include <stdio.h>
//from w w w . ja v a 2 s . com
jmp_buf ebuf;
void f2(void);
int main(void)
{
int i;
printf("1 ");
i = setjmp(ebuf);
if(i == 0) {
f2();
printf("This will not be printed.");
}
printf("%d", i);
return 0;
}
void f2(void)
{
printf("2 ");
longjmp(ebuf, 3);
}
The code above generates the following result.