setjmp and longjmp

longjmp:-

-non local jump to a saved context


Synopsis:-

#include<setjmp.h>
void longjmp(jmp_buf env,int val);


Description:-

longjmp() and setjmp() are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. longjmp() restores the environment saved by the last call of setjmp() with the corresponding env argument. After longjmp() is completed, program execution continues as if the corresponding call of setjmp()had just returned the value val. longjmp() cannot cause 0 to be returned. If longjmp is invoked with a second argument of 0,  1will be returned instead.

This function never returns a value



setjmp:-

-Saves stack context for nonlocal goto


SYNOPSIS

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


DESCRIPTION

setjmp and longjmp are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. setjmp()saves the stack context/environment in env for later use by longjmp(). The stack context will be invalidated if the
function which called setjmp() returns.


RETURN VALUE

It returns the value 0 if returning directly and non-zero when returning from longjmp() using the saved context.

Program:- 
Output:-
i = 0
i = 2

Comments