[Add] 新增setjmp测试用例

This commit is contained in:
gaoyang3513
2024-06-27 22:44:18 +08:00
parent 7455f611b8
commit b9aee7b5e8

26
02-setjmp/jmp.c Normal file
View File

@ -0,0 +1,26 @@
#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;
}