Files
Linux_C/02-setjmp/jmp.c
2025-03-23 16:48:18 +08:00

27 lines
503 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}