- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
setjump() and longjump() in C
In this section, we will see what are the setjump and longjump in C. The setjump() and longjump() is located at setjmp.h library. The syntax of these two functions is like below.
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.
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
}Output
5555555555555555555555555555555555555555555555555555555555555555555555555555 555555555555555555555555
- Related Articles
- Comma in C and C++
- Loops in C and C++
- Foreach in C++ and C#
- INT_MAX and INT_MIN in C/C++ and Applications
- isalpha() and isdigit() in C/C++
- nextafter() and nexttoward() in C/C++
- Undefined Behaviour in C and C++
- rand() and srand() in C/C++
- Floating Point Operations and Associativity in C, C++ and Java
- # and ## Operators in C ?
- exit(), abort() and assert() in C/C++
- strdup() and strdndup() in C/C++\n
- Name Mangling and extern “C” in C++
- Difference between Structures in C and C++
- Alternating Vowels and Consonants in C/C++
Advertisements
