首頁 >後端開發 >C++ >在C語言中,setjump()和longjump()函數的翻譯如下:

在C語言中,setjump()和longjump()函數的翻譯如下:

WBOY
WBOY轉載
2023-08-30 08:21:111693瀏覽

在C語言中,setjump()和longjump()函數的翻譯如下:

在這個部分,我們將會看到C語言中的setjump和longjump是什麼。 setjump()和longjump()位於setjmp.h庫中。這兩個函數的語法如下所示。

setjump(jmp_buf buf) : uses buf to store current position and returns 0.
longjump(jmp_buf buf, i) : Go back to place pointed by buf and return i.

These are used in C for exception handling. The setjump() can be used as try block, and longjump() can be used as throw statement. The longjump() transfers control the pointed whichby is setjump().

Here we will see how to print a number 100 times without using recursion, loop, or macro expansion. Here we will use the setjump() and longjump() functions to do that.

##Example

#include <stdio.h>
#include <setjmp.h>
jmp_buf buf;
main() {
   int x = 1;
   setjmp(buf); //set the jump position using buf
   printf("5"); // Prints a number
   x++;
   if (x <= 100)
      longjmp(buf, 1); // Jump to the point located by setjmp
}

輸出

5555555555555555555555555555555555555555555555555555555555555555555555555555
555555555555555555555555

以上是在C語言中,setjump()和longjump()函數的翻譯如下:的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除