“起跳”和“跳远”的定义见 setjmp。H ,C标准库中的头文件。
null
- setjump(jmp_buf buf) :使用buf记住当前位置并返回0。
- 跳远(jmp_buf buf,i) :返回buf指向的位置并返回i。
// A simple C program to demonstrate working of setjmp() and longjmp() #include<stdio.h> #include<setjmp.h> jmp_buf buf; void func() { printf ( "Welcome to GeeksforGeeks" ); // Jump to the point setup by setjmp longjmp (buf, 1); printf ( "Geek2" ); } int main() { // Setup jump position using buf and return 0 if ( setjmp (buf)) printf ( "Geek3" ); else { printf ( "Geek4" ); func(); } return 0; } |
输出:
Geek4 Welcome to GeeksforGeeks Geek3
这些函数的主要功能是提供一种偏离标准调用和返回顺序的方法。这主要用于在C中实现异常处理。 setjmp 可以像这样使用 尝试 (如C++语言和java语言)。呼吁 朗吉姆普 可以像这样使用 扔 (请注意,longjmp()将控制转移到setjmp()设置的点)。
本文由 阿迪蒂亚·查特吉 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END