我试图把代码写的清晰一些!
让大家在看的过程中能直接看出原理
#include <setjmp.h>
#include <stdio.h>
jmp_buf buf;
int main(int argc, char *argv[])
{
int nValue = 0;
printf("Function Start\r\n");
nValue = setjmp(buf);//第一次调用会返回0 //以后返回Longjmp设定的值
switch(nValue)
{
case 0:
{
printf("nValue = %d \r\n",nValue);
longjmp(buf,1); //设置一次
break;
}
case 1:
{
printf("nValue = %d \r\n",nValue);
longjmp(buf,2); // 再次设置一次
break;
}
case 2:
printf("nValue = %d \r\n",nValue);
break;
}
printf("Function End\r\n");
return 0;
}
显示运行结果
Function Start
nValue = 0
nValue = 1
nValue = 2
Function End