jweinst1
8/5/2017 - 7:41 AM

setjmp experiment in c

setjmp experiment in c

/* setjmp_longjmp.c -- program handles error through 'setjmp()' */
/* and longjmp() */
//practice for error handling
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
 
/* declare variable of type jmp_buf */
static jmp_buf resume_here;
 
void hello(void);
 
int main(void)
{
  printf("startng up\n");
  //first time does not activate
  if(setjmp(resume_here)) {caller(); return 0;};
  
  longjmp(resume_here, 1);
  printf("Cannot be reached");
  return 0;
}

void caller()
{
  printf("Just resumed in caller, now going back\n");
}