Mercurial > hg > Members > kaito > longjump
view factorial.c @ 3:e6aa3b678e4a
remove ret from global_longjump.c
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 12 Nov 2013 15:22:04 +0900 |
parents | dee9711aeb06 |
children |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <setjmp.h> __code print_factorial(int prod) { printf("factorial = %d\n",prod); exit(0); } __code factorial0(int prod, int x) { if ( x >= 1) { goto factorial0(prod*x, x-1); }else{ goto print_factorial(prod); } } int factorial(int x) { void *__return; void *__enviroment; printf("factorial entry\n"); __enviroment = (void*)malloc(sizeof(jmp_buf)); if (setjmp(__enviroment)){ free(__enviroment); printf("factorial return\n"); return 0; } __return = (void*)return1; goto factorial0(1, x); return -1; } int main(int argc, char **argv) { int i,ans; i = atoi(argv[1]); ans = factorial(i); printf("answer = %d\n",ans); }