[Add] 新增setjmp测试用例
This commit is contained in:
26
02-setjmp/jmp.c
Normal file
26
02-setjmp/jmp.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user