Error Handling in C
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.

Conquer recursion in C: define recursive functions, master base and recursive cases, explore factorial, Fibonacci, tree traversal, and understand stack behavior.
Recursion is a powerful programming technique where a function calls itself to solve a problem. It can be a bit tricky to grasp at first, but once you understand the concept, it becomes a valuable tool for solving complex problems. In this tutorial, we'll explore recursion in C language with detailed explanations and examples.
Table of contents [Show]
Recursion is a technique where a function calls itself in order to solve a problem. It is a common concept in computer science and is used to solve problems that can be broken down into smaller, similar sub-problems. Recursive functions have two main parts: the base case and the recursive case.
To successfully implement recursion, you must follow three fundamental laws:
The base case is a condition that determines when the recursion should stop. Without a base case, the recursive function would keep calling itself indefinitely, resulting in a stack overflow.
In each recursive call, the problem should become smaller or simpler in some way. This ensures that the function eventually reaches the base case.
The recursive function must call itself to solve a smaller version of the problem. This is the essence of recursion.
To illustrate these concepts, let's look at a simple example: calculating the factorial of a number.
The factorial of a non-negative integer n (denoted as n!) is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120.
int factorial(int n) {
// Base case: if n is 0 or 1, return 1
if (n == 0 || n == 1) {
return 1;
}
// Recursive case: multiply n by factorial of (n-1)
else {
return n * factorial(n - 1);
}
}
In this example:
n becomes 0 or 1, the function returns 1.n * factorial(n - 1).Let's explore more examples of recursive functions.
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. It starts with 0 and 1, and the sequence continues as 0, 1, 1, 2, 3, 5, 8, 13, ...
int fibonacci(int n) {
// Base case: if n is 0 or 1, return n
if (n == 0 || n == 1) {
return n;
}
// Recursive case: calculate fibonacci(n-1) + fibonacci(n-2)
else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
The Tower of Hanoi is a classic puzzle that involves moving a stack of disks from one peg to another, with the constraint that a larger disk cannot be placed on top of a smaller one.
void towerOfHanoi(int n, char source, char auxiliary, char destination) {
// Base case: If there's only one disk, move it from source to destination
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
// Move n-1 disks from source to auxiliary using destination as a helper peg
towerOfHanoi(n - 1, source, destination, auxiliary);
// Move the remaining disk from source to destination
printf("Move disk %d from %c to %c\n", n, source, destination);
// Move the n-1 disks from auxiliary to destination using source as a helper peg
towerOfHanoi(n - 1, auxiliary, source, destination);
}
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.
Unlock flexibility in C—learn how to implement variadic functions using stdarg.h: handling ellipses (...), va_list, va_start, va_arg, va_end, with clear examples.
Unlock the power of C preprocessors — learn directives like #include, #define, macros, conditional compilation, and file inclusion for cleaner, flexible code.
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.


