setjmp.h in C

setjmp.h in C

The file header named setjmp.h in C Library defines the macro setjmp (), a longjmp () function, and a variable type jmp_buf, to ignore the regular function call and return the rule, by providing methods mode to perform jumps while maintaining the function call environment.

The variable is defined in setjmp.h

Here is the type of variable defined in setjmp.h:

jmp_buf : This is an array type used to hold information for the setjmp () macro and longjmp () function.

The macros are defined in setjmp.h

There is only one macro defined in this library:

int setjmp (jmp_buf environment) : This macro stores the current environment inside the environment variable for later use by longjmp (). If this macro returns directly from the macro call, it returns 0; but if it returns from a call to longjmp (), a value other than 0 is returned.

Macro declaration setjmp () in C

Below is the declaration for setjmp () macro.

 int setjmp (jmp_buf environment) 

Parameters:

environment - This is an object of type jmp_buf where environment information is stored.

Return value:

This macro returns more than 1 time. First, on its direct call, it always returns 0. When longjmp is called with the information set to the environment, this macro returns again; this time it returns the value passed to longjmp as the second parameter.

For example:

The following C program illustrates the usage of setjmp () macro.

 #include 
#include
#include

main ()
{
jmp_buf env;
int i;

i = setjmp (env);
printf ("i =% dn", i);

if (i! = 0) exit (0);

longjmp (env, 2);
printf ("Is it printed now?");

}

Compiling and running the above C program will result:

 i = 0 
i = 2

The functions are defined in setjmp.h

Only one function is defined in setjmp.h:

Function longjmp void (jmp_buf environment, int value): This function restores the environment (environment) stored by the closest call to the setjmp () macro in the same function call of the program with the corresponding parameter jmp_buf.

Declare the function longjmp () in C

Below is the declaration for longjmp () function in C:

 void longjmp (jmp_buf environment, int value) 

Parameters:

environment - This is an object of type jmp_buf that contains information to store the environment at the call point of setjmp.
value - This is the value for the setjmp expression to estimate.

Return value:

This function does not return any values.

For example:

The following program C illustrates the usage of the longjmp () function in C:

 #include 
#include
#include

main ()
{
jmp_buf env;
int i;

i = setjmp (env);
printf ("i =% dn", i);

if (i! = 0) exit (0);

longjmp (env, 2);
printf ("Is it printed now?");

}

Compiling and running the above C program will result:

 i = 0 
i = 2

According to Tutorialspoint

Last lesson: math.h in C

Next post: signal.h in C

4 ★ | 1 Vote

May be interested

  • signal.h in CPhoto of signal.h in C
    the header file named signal.h in library c defines a type of variable sig_atomic_t, two function calls, and a number of macros to handle the different signals reported during execution of a program.
  • Function raise () in CPhoto of Function raise () in C
    function int raise (int sig) in library c creates sig signal. the sig parameter is compatible with the sig macro.
  • Function signal () in CPhoto of Function signal () in C
    void function (* signal (int sig, void (* func) (int)) (int) in library c establishes a function for signal processing (eg a signal handler).
  • stdarg.h in CPhoto of stdarg.h in C
    the file header named stdarg.h in library c defines a type of va_list variable and 3 macros that can be used to retrieve parameters in a function when the number of parameters is unknown (eg the number of parameters is can change).
  • stddef.h in CPhoto of stddef.h in C
    the file header named stddef.h in c library defines various types of variables and macros. many of these definitions are also present in other headers.
  • stdio.h in CPhoto of stdio.h in C
    the header file named stdio.h in standard c library defines three variable types, a number of macros and various functions to implement input and output.