Skip to content
Related Articles
Open in App
Not now

Related Articles

C setjump and longjump

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 11 Mar, 2023
Improve Article
Save Article

“Setjump” and “Longjump” are defined in setjmp.h, a header file in C standard library.

  • setjump(jmp_buf buf) : uses buf to remember the current position and returns 0.
  • longjump(jmp_buf buf, i) : Go back to the place buf is pointing to and return i.


// C program to demonstrate 
// working of setjmp() and
// longjmp()
#include <setjmp.h>
#include <stdio.h>
jmp_buf buf;
void func()
    printf("Welcome to GeeksforGeeks\n");
    // Jump to the point setup by setjmp
    longjmp(buf, 1);
int main()
    // Setup jump position using buf and return 0
    if (setjmp(buf))
    else {
    return 0;


Welcome to GeeksforGeeks

The main feature of these functions is to provide a way that deviates from standard call and return sequences. This is mainly used to implement exception handling in C. setjmp can be used like try (in languages like C++ and Java). The call to longjmp can be used like throw (Note that longjmp() transfers control to the point set by setjmp()). 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!