Home > Backend Development > C++ > In C language, the setjump() and longjump() functions are translated as follows:

In C language, the setjump() and longjump() functions are translated as follows:

WBOY
Release: 2023-08-30 08:21:11
forward
1716 people have browsed it

In C language, the setjump() and longjump() functions are translated as follows:

In this section, we will see what setjump and longjump are in C language. setjump() and longjump() are located in the setjmp.h library. The syntax of these two functions is as follows.

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.
Copy after login

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 pointe which is pointed by 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
}
Copy after login

Output

5555555555555555555555555555555555555555555555555555555555555555555555555555
555555555555555555555555
Copy after login

The above is the detailed content of In C language, the setjump() and longjump() functions are translated as follows:. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template