setjmp code example

Example 1: setjmp

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>

int main (void) {
    jmp_buf env;
    int val;

    /* Ortamı kaydetme */
    val = setjmp(env);

    if(val!=0) {
       printf("Bu noktaya longjmp() fonksiyonu ile ve %d değeri ile erişim sağlandı!", val);
       exit(0);
    }

    printf("longjmp() fonksiyon çağrısı.\n");
    longjmp(env, 7);

    return 0;
}

Example 2: setjmp

#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <stdlib.h>#include <errno.h>#include <stdio.h>#include <setjmp.h> static jmp_buf env; static void f2(void){	printf("f2... \n"); 	longjmp(env, 2);} static void f1(int argc){	printf("f1... \n");	if (argc == 1)		longjmp(env, 1); 	// Return to the place where setjmp was called. At this time, because env 1 is set, setjmp will return 1.	 f2 (); // Return above, f2 does not execute} int main(int argc, char *argv[]){	switch (setjmp(env)) {		 case 0: / * returns 0 after calling setjmp (), execute the following * /			printf("Calling f1() after initial setjmp()\n");			f1(argc);              		break;                  		 case 1: // longjmp (env, 1); setjmp will return 1, execute here			printf("We jumped back from f1()\n");		break; 		case 2:			printf("We jumped back from f2()\n");		break;				default:			printf("default \n");		break;	} 	return 0;}

Tags:

Misc Example