//展示函数setjmp()和longjmp()的用法
#include <setjmp.h>
#include "tlpi_hdr.h"
#define EXIT_SUCESS 0
static jmp_buf env;
static void
f2(void)
{
longjmp(env,2);
}
static void
f1(int argc)
{
if (argc == 1)
longjmp(env,1);
f2();
}
int
main(int argc,char *argv[])
{
switch (setjmp(env)) {
case 0: /*This is the return after after the initial setjmp()*/
printf("Calling f1() after initial setjmp()\n");
f1(argc); /*Nerver returns...*/
break; /*...but this is good form*/
case 1:
printf("We jumped back from f1()\n");
break;
case 2:
printf("We jumped back from f2()\n");
break;
}
exit(EXIT_SUCCESS);
}
/*
程序使用示例:
[root@localhost linux-test]# gl++ test.c
[root@localhost linux-test]# ./a.out
Calling f1() after initial setjmp()
We jumped back from f1()
[root@localhost linux-test]# ./a.out 1
Calling f1() after initial setjmp()
We jumped back from f2()
*/
展示函数setjmp()和longjmp()的用法
最新推荐文章于 2021-12-27 13:48:58 发布
本文介绍了C语言中的setjmp函数和longjmp函数的用法,通过实例展示了如何在函数间进行非局部跳转和错误处理。通过调用setjmp设置一个环境,longjmp则用于在不同函数间返回到之前设定的位置。
192

被折叠的 条评论
为什么被折叠?



