Save the processor state for longjmp().
This function is compatible with the following targets:
|
|
#include <setjmp.h>
int setjmp(jmp_buf env);Parameters:
Parameters for this facility are:
WARNING!
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
// Let main() and doerr() both have
// access to global env
volatile jmp_buf env;
void doerr(void);
int main(void)
{
int i, j, k;
printf("Enter 3 integers that total less than 100.\n");
printf("A zero sum will quit.\n\n");
// If the total of entered numbers is not less than 100,
// program execution is restarted from this point.
if (setjmp(env) != 0)
printf("Try again, please.\n");
do {
scanf("%d %d %d", &i, &j, &k);
if ( (i + j + k) == 0)
exit(0); // quit program
printf("%d + %d + %d = %d\n\n", i, j, k, i+j+k);
if ( (i + j + k) >= 100)
doerr(); // error!
} while (1); // loop forever
return 0;
}
void doerr(void) // this is the error handler
{
printf("The total is >= 100!\n");
longjmp(env, 1);
}
Output:
Enter 3 integers that total less than 100.
A zero sum will quit.
10 20 30
10 + 20 + 30 = 60
-4 5 1000
-4 + 5 + 1000 = 1001
The total is >= 100!
Try again, please.
0 0 0