0008-longjmp.c (471B)
1 #include <setjmp.h> 2 #include <stdio.h> 3 4 /* 5 output: 6 test 1 7 i = 1, v = 2 8 test 2 9 i = 2, v = 3 10 end: 11 */ 12 13 jmp_buf recover; 14 15 void 16 test(char *s, int val) 17 { 18 puts(s); 19 longjmp(recover, val); 20 } 21 22 int 23 main() 24 { 25 static int i; 26 auto volatile int v; 27 28 i = 0; 29 v = 1; 30 if (!setjmp(recover)) { 31 i = 1; 32 v = 2; 33 test("test 1", 1); 34 } 35 printf("i = %d, v = %d\n", i, v); 36 37 if (!setjmp(recover)) { 38 i = 2; 39 v = 3; 40 test("test 2", 0); 41 } 42 printf("i = %d, v = %d\n", i, v); 43 44 return 0; 45 }