Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:28:25+00:00 2026-06-18T08:28:25+00:00

Can anyone explain me where exactly setjmp() and longjmp() functions can be used practically

  • 0

Can anyone explain me where exactly setjmp() and longjmp() functions can be used practically in embedded programming? I know that these are for error handling. But I’d like to know some use cases.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:28:26+00:00Added an answer on June 18, 2026 at 8:28 am

    Error handling
    Suppose there is an error deep down in a function nested in many other functions and error handling makes sense only in the top level function.

    It would be very tedious and awkward if all the functions in between had to return normally and evaluate return values or a global error variable to determine that further processing doesn’t make sense or even would be bad.

    That’s a situation where setjmp/longjmp makes sense.
    Those situations are similar to situation where exception in other langages (C++, Java) make sense.

    Coroutines
    Besides error handling, I can think also of another situation where you need setjmp/longjmp in C:

    It is the case when you need to implement coroutines.

    Here is a little demo example.
    I hope it satisfies the request from Sivaprasad Palas for some example code and answers the question of TheBlastOne how setjmp/longjmp supports the implementation of corroutines (as much as I see it doesn’t base on any non-standard or new behaviour).

    EDIT:
    It could be that it actually is undefined behaviour to do a longjmp down the callstack (see comment of MikeMB; though I have not yet had opportunity to verify that).

    #include <stdio.h>
    #include <setjmp.h>
    
    jmp_buf bufferA, bufferB;
    
    void routineB(); // forward declaration 
    
    void routineA()
    {
        int r ;
    
        printf("- 12 : (A1)\n");
    
        r = setjmp(bufferA);
        if (r == 0) routineB();
    
        printf("- 17 : (A2) r=%d\n",r);
    
        r = setjmp(bufferA);
        if (r == 0) longjmp(bufferB, 20001);
    
        printf("- 22 : (A3) r=%d\n",r);
    
        r = setjmp(bufferA);
        if (r == 0) longjmp(bufferB, 20002);
    
        printf("- 27 : (A4) r=%d\n",r);
    }
    
    void routineB()
    {
        int r;
    
        printf("- 34 : (B1)\n");
    
        r = setjmp(bufferB);
        if (r == 0) longjmp(bufferA, 10001);
    
        printf("- 39 : (B2) r=%d\n", r);
    
        r = setjmp(bufferB);
        if (r == 0) longjmp(bufferA, 10002);
    
        printf("- 44 : (B3) r=%d\n", r);
    
        r = setjmp(bufferB);
        if (r == 0) longjmp(bufferA, 10003);
    }
    
    
    int main(int argc, char **argv) 
    {
        routineA();
        return 0;
    }
    
    

    Output

    - 12 : (A1)
    - 34 : (B1)
    - 17 : (A2) r=10001
    - 39 : (B2) r=20001
    - 22 : (A3) r=10002
    - 44 : (B3) r=20002
    - 27 : (A4) r=10003
    

    Following figure shows the flow of execution:
    flow of execution

    Warning note
    When using setjmp/longjmp be aware that they have an effect on the validity of local variables often not considered.
    Cf. my question about this topic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain what exactly the Android Backup API is used for? I have
Can anyone explain me briefly what exactly is component programming in .NET and why
can anyone explain me these codes what are they doing exactly? I could not
Can anyone explain me how to overpass these problems? Description Resource Path Location Type
Can anyone explain what the jquery documentation is exactly referring to with this statement:
*Can anyone exactly explain what the function '__raw_writel' do and what are the arguments
Can anyone please explain exactly what this code and its components are doing? I
Can anyone please explain exactly how the following code works, line by line. I'm
Can anyone explain, what exactly happens in the following line? java.lang.System.out.print(string + i); It
Can anyone explain exactly why Method 1 in the following code does not alter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.