Introduction: When writing a program, often use the goto statement inside the function to jump, so that the error processing, if you want to jump between functions what to do? Use the setjmp and LONGJMP functions.
To show an example program:
#include <stdio.h> #include <stdlib.h> #include <setjmp.h>static void F1 (int, int, int, int), static void F2 (void); static jmp_buf jmpbuffer;static int Globval;intmain (void) {int autoval;register int regival;volatile int volaval;static int statval;globval = 1;autoval = 2;regival = 3; Volaval = 4;statval = 5;if (setjmp (jmpbuffer)! = 0) {printf ("after longjmp:\n");p rintf ("global =%d, Autoval =%d, regival =%d, Volaval =%d, Statval =%d\n ", Globval, Autoval, Regival, Volaval, Statval); exit (0);} /* * Change variable after setjmp, buf before longjmp. */globval = 95;autoval = 96;regival = 97; Volaval = 98;statval = 99;f1 (Autoval, Regival, Volaval, statval);/*never returns*/exit (0);} static VOIDF1 (int i, int j, int k, int l) {printf ("in F1 (): \ n");p rintf ("global =%d, Autoval =%d, Regival =%d, Volaval = %d, Statval =%d\n ", Globval,i, J, K, L); F2 (); Static voidf2 (void) {longjmp (Jmpbuffer, 1);}
To compile the program test results:
In F1 (): global = Autoval = Regival, volaval = 98, Statval = 99after Longjmp:global = up, Autoval = +, re Gival = Volaval = 98, Statval = 99