27 lines
503 B
C
27 lines
503 B
C
#include <setjmp.h>
|
||
#include <stdio.h>
|
||
|
||
jmp_buf env;
|
||
|
||
void second() {
|
||
printf("Inside second function\n");
|
||
longjmp(env, 1); // 跳回setjmp的地方,并且setjmp返回1
|
||
}
|
||
|
||
void first() {
|
||
printf("Inside first function\n");
|
||
second();
|
||
printf("This line will not be printed\n"); // 不会执行到这里
|
||
}
|
||
|
||
int main() {
|
||
if (setjmp(env) == 0) {
|
||
// 第一次调用setjmp时返回0
|
||
first();
|
||
} else {
|
||
// 从longjmp返回时,setjmp将返回非零值
|
||
printf("Back to main\n");
|
||
}
|
||
|
||
return 0;
|
||
} |