Recursion
Recursion is a function calling itself, directly or through a cycle of other functions. Each call gets a new stack frame. The chain has to stop, or the stack runs out.
Detailed Explanation
A recursive solution has two parts. The base case returns without calling the function again. The recursive case reduces the problem and calls the same function on the smaller input. Factorial, walking a tree, and descending a file-system directory are the usual teaching examples. In C the same pattern shows up in parsers, in graph searches, and in anything that is already a tree in memory.
Each call consumes stack. Locals, parameters, and the return address live in the frame. On Linux the main thread often has about 8 MB. A pthread may have less. A recursion that is one frame per input element will die on a long linked list long before a loop would notice. Tail-call elimination — reusing the current frame when the recursive call is the last thing the function does — is a compiler courtesy, not a C guarantee. Write a loop if the depth is unbounded.
Indirect recursion is the same idea with more names: a calls b, b calls a. Mutual recursion shows up in recursive-descent parsers (expression calls term calls expression). You still need a base case, and you still pay a frame per call.
C does not provide a stack inspector. Overflow is undefined behavior. On a hosted system with guard pages you usually get SIGSEGV. On a freestanding target you silently overwrite adjacent memory. If the input can be hostile — a crafted deeply nested JSON document, a filesystem with a loop of symlinks — bound the depth yourself and fail, or switch to an explicit heap-allocated stack.
Recursion is the right tool when the data is nested and the depth is small or naturally bounded: an AST, a balanced tree, a board-game search with a ply limit. It is the wrong tool for a linear scan. The iterative rewrite is usually shorter in C, because you already manage memory by hand and an explicit stack is one more struct you understand.
Code Examples
#include <stdio.h>
/* Depth equals n. Fine for n = 10, fatal for n = 10 million. */
static unsigned long factorial(unsigned n)
{
if (n < 2) {
return 1; /* base case */
}
return (unsigned long)n * factorial(n - 1);
}
int main(void)
{
printf("%lu\n", factorial(6)); /* prints: 720 */
return 0;
}#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
struct Node *left;
struct Node *right;
};
static void walk_inorder(const struct Node *n)
{
if (n == NULL) { /* base case: empty subtree */
return;
}
walk_inorder(n->left);
printf("%d ", n->value);
walk_inorder(n->right);
}
static struct Node *leaf(int value)
{
struct Node *n = malloc(sizeof *n);
if (n == NULL) {
return NULL;
}
n->value = value;
n->left = n->right = NULL;
return n;
}
int main(void)
{
struct Node *root = leaf(2);
if (root == NULL) {
return 1;
}
root->left = leaf(1);
root->right = leaf(3);
walk_inorder(root); /* prints: 1 2 3 */
printf("\n");
free(root->left);
free(root->right);
free(root);
return 0;
}Common Gotchas
There is no portable stack overflow handler
a deep recursion is a crash or silent corruption. Bound the depth or iterate.
Tail-call optimization is not required
`return f(n - 1);` may still allocate a frame. Do not design APIs that need TCO to be safe.
A missing base case is an infinite recursion
the compiler will not catch `factorial` calling `factorial(n)` by mistake. Test the smallest inputs first.
Mutual recursion needs a forward declaration
declare `static void b(void);` before `a` so `a` can call `b`.