setjmp() and longjmp() Functions in C Library

Question: What are setjmp() and longjmp() Functions in Standard C Library. Explain with Examples When to Use them

Answer: ‘setjmp()’ and ‘longjmp()’ functions provide a powerful mechanism similar to ‘goto’ but not restricted to just current function. This mechanism is useful in situations where there’s a deeply nested function calls and when error gets detected, in some lower-level function, then control immediately transfers to top-level function without having to return and check for error code by intermediate functions. These functions work in pairs as follows:

1. setjmp(jmp_buf resume_here) must be called first. This initializes ‘resume_here’ with program state information viz. value of pointer to the top of the stack, value of program counter and some other info. It returns ZERO when returns first time.

2. longjmp(jmp_buf resume_here, int i) is then called. This says go back to place where to resume from defined by ‘resume_here’. This makes it look like you’re returning from original ‘setjmp()’ but return value of integer ‘i’ to let code tell when you actually got back here via ‘longjmp()’.

3. After longjmp()’ed, contents of ‘jmp_buf’ variable ‘resume_here’ destroys.

Let’s take a C program to learn how to use them.

advertisement
advertisement
/* setjmp_longjmp.c -- program handles error through 'setjmp()' */
/* and longjmp() */
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
 
/* declare variable of type jmp_buf */
jmp_buf resume_here;
 
void hello(void);
 
int main(void)
{
    int ret_val;
 
    /* Initialize 'resume_here' by calling setjmp() */
    if (setjmp(resume_here)) {
        printf("After \'longjump()\', back in \'main()\'\n");
        printf("\'jump buffer variable \'resume_here\'\' becomes "
                  "INVALID!\n");
    }
    else {
        printf("\'setjmp()\' returns first time\n");
        hello();
    }
 
    return 0;
}
 
void hello(void)
{
    printf("Hey, I'm in \'hello()\'\n");
    longjmp(resume_here, 1);
 
    /* other code */
    printf("can't be reached here because I did longjmp!\n");
}

Output is written below

'setjmp()' returns first time
Hey, I'm in 'hello()'
After 'longjump()', back in 'main()'
'jump buffer variable 'resume_here'' becomes INVALID!

Notice that when ‘setjmp()’ called first time, returned value ZERO and hence ‘else clause’ executed. Within there, after printing the msg “‘setjmp()’ returns first time” called function ‘hello()’. In ‘hello()’ function, ‘longjmp()’ called which caused control immediately transferred back to ‘setjmp()’ in the ‘main()’. This time, ‘setjmp()’ returned value 1 passed to it by ‘longjmp()’ and so ‘if clause’ executed.

Note: Join free Sanfoundry classes at Telegram or Youtube

A ‘goto’ can’t jump out of current function while we can ‘longjmp’ long way away, even to a function in a different file.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.