C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

What is the difference between goto and longjmp() and setjmp()?

0 votes
32 views
asked Apr 8, 2014 by sunny Expert (11,410 points)

1 Answer

0 votes
A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution.
Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.
A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions.
When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.
answered Apr 8, 2014 by sara Guru (24,570 points)

Related questions

0 votes
1 answer 37 views
37 views asked Apr 8, 2014 by rock Expert (14,990 points)
0 votes
1 answer 24 views
24 views asked Apr 7, 2014 by rock Expert (14,990 points)
0 votes
1 answer 18 views
0 votes
1 answer 11 views
...