Posted By

Feb30th1712 on 01/13/08


Tagged

c demo setjmph setjmp longjmp


Versions (?)

setjmp


 / Published in: C
 

  1. #include <setjmp.h>
  2. #include <stdio.h>
  3.  
  4. jmp_buf envBuf; //environment buffer
  5.  
  6. int main(void)
  7. {
  8. int dividend, divisor;
  9.  
  10. if ( setjmp(envBuf) )
  11. {
  12. puts("Enter a number.");
  13. getchar();
  14. }
  15. printf("Dividend: ");
  16. if (scanf("%d", &dividend) != 1) longjmp(envBuf, 1);
  17.  
  18. switch ( setjmp(envBuf) )
  19. {
  20. case 0: break;
  21. case 1: puts("Enter a number."); getchar(); break;
  22. case 2: puts("Divided by zero."); break;
  23. default: puts("Unknown error."); break;
  24. }
  25. printf("Divisor: ");
  26. if (scanf("%d", &divisor) != 1) longjmp(envBuf, 1);
  27. if (divisor == 0) longjmp(envBuf, 2);
  28. printf("%d / %d = %d", dividend, divisor, dividend / divisor);
  29.  
  30. return 0;
  31. }

Report this snippet  

You need to login to post a comment.