#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
static jmp_buf env;
void d(void)
{
fprintf(stdout,"%s\n",__func__);
fprintf(stdout,"d end\n");
longjmp(env, 10); //跳转到设置点
}
void c(void)
{
fprintf(stdout,"%s\n",__func__);
fprintf(stdout,"c begin\n");
d();
fprintf(stdout,"c end\n");
}
void b(void)
{
fprintf(stdout,"%s\n",__func__);
fprintf(stdout,"b begin\n");
c();
fprintf(stdout,"b end\n");
}
void a(void)
{
int ret;
ret = setjmp(env); //设置跳转点
if(ret ==0) //跳转前执行函数
{
fprintf(stdout, "setjmp ok\n");
fprintf(stdout,"%s\n",__func__);
fprintf(stdout,"a begin\n");
b();
fprintf(stdout,"a end\n");
}
else if(ret !=0) //跳转后执行函数
{
fprintf(stdout, "val:%d\n", ret);
}
}
int main()
{
fprintf(stdout,"%s\n",__func__);
fprintf(stdout,"main begin\n");
a();
fprintf(stdout,"main end\n");
exit(0);
}
标签:__,longjmp,stdout,void,setjmp,fprintf,func,跳转 From: https://www.cnblogs.com/linux-learn/p/18541151