Latest web development tutorials

C library macro - setjmp ()

C standard library - <setjmp.h> C standard library - <setjmp.h>

description

C library Macroint setjmp (jmp_buf environment) stored in the current environment variable environment in order to function longjmp ()for subsequent use. If this macro returns directly from the macro call, it returns zero, but if it returns fromlongjmp () function call, it returns a longjmp passed as the second argument of a nonzero value.

statement

Here is the setjmp () macro statement.

int setjmp(jmp_buf environment)

parameter

  • environment - this is the object used to store environment information of a type jmp_buf.

return value

This macro may return more than once. The first time, in a direct call it, it always returns zero. With environmental information provided when you call longjmp, this macro will return again, at which point it returns the value will be passed as the second argument to longjmp.

Examples

The following example demonstrates the setjmp () macro usage.

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>

int main()
{
   int val;
   jmp_buf env_buffer;

   /* 保存 longjmp 的调用环境 */
   val = setjmp( env_buffer );
   if( val != 0 ) 
   {
      printf("从 longjmp() 返回值 = %s\n", val);
      exit(0);
   }
   printf("跳转函数调用\n");
   jmpfunction( env_buffer );
   
   return(0);
}

void jmpfunction(jmp_buf env_buf)
{
   longjmp(env_buf, "w3cschool.cc");
}

Let's compile and run the above program, which will result in the following:

跳转函数调用
从 longjmp() 返回值 = w3cschool.cc

C standard library - <setjmp.h> C standard library - <setjmp.h>