Skip to content

Instantly share code, notes, and snippets.

@huwan
Created July 6, 2015 01:59
Show Gist options
  • Save huwan/645b7b173cc65f6488ef to your computer and use it in GitHub Desktop.
Save huwan/645b7b173cc65f6488ef to your computer and use it in GitHub Desktop.
setjmp和longjmp函数使用
// http://blog.csdn.net/chenyiming_1990/article/details/8683413
#include <stdio.h>
#include <setjmp.h>
static jmp_buf buf;
void second(void) {
printf("second\n"); // 打印
longjmp(buf,1); // 跳回setjmp的调用处 - 使得setjmp返回值为1
}
void first(void) {
second();
printf("first\n"); // 不可能执行到此行
}
int main() {
if ( ! setjmp(buf) ) {
first(); // 进入此行前,setjmp返回0
} else { // 当longjmp跳转回,setjmp返回1,因此进入此行
printf("main\n"); // 打印
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment