Skip to main content

Lesson 4 of 8

Recursion - Call Stack Depth & Tail Calls

Recursion is a function calling itself. Each recursive call creates a new stack frame. **Stack overflow** occurs when recursion depth exceeds the stack size (typically 1-8 MB on Linux). Understanding when recursion is appropriate - and when iterative or tail-recursive solutions are better - is essential for writing robust C code.

13 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Recursion - Factorial, Fibonacci & Stack Depth

Recursion - Factorial, Fibonacci & Stack Depth
#include <stdio.h>

/* ── Base recursion: factorial ──────────────────────────────
   factorial(5) = 5 * factorial(4)
                    = 4 * factorial(3)
                         = 3 * factorial(2)
                              = 2 * factorial(1)
                                   = 1 (BASE CASE)
   Stack depth = n frames. factorial(100000) → STACK OVERFLOW.  */
long long factorial(int n) {
    if (n <= 1) return 1;          /* base case - MUST exist */
    return n * factorial(n - 1);  /* recursive case */
}

/* ── Tree recursion: Fibonacci - exponentially bad ──────────
   fib(n) calls fib(n-1) AND fib(n-2) - TWO recursive calls.
   fib(40) makes ~2^40 function calls. Never use this in production.
   Use memoization or an iterative version.                    */
long long fib_naive(int n) {
    if (n <= 1) return n;
    return fib_naive(n - 1) + fib_naive(n - 2);
}

/* ── Iterative Fibonacci - O(n) time, O(1) space ─────────── */
long long fib_iter(int n) {
    if (n <= 1) return n;
    long long a = 0, b = 1;
    for (int i = 2; i <= n; i++) {
        long long tmp = a + b;
        a = b;
        b = tmp;
    }
    return b;
}

/* ── Binary search - recursive form ─────────────────────── */
int bsearch_rec(const int *arr, int lo, int hi, int target) {
    if (lo > hi) return -1;           /* base case: not found */
    int mid = lo + (hi - lo) / 2;
    if (arr[mid] == target) return mid;
    if (arr[mid]  < target) return bsearch_rec(arr, mid+1, hi, target);
    return                         bsearch_rec(arr, lo, mid-1, target);
}

/* ── Mutual recursion - two functions calling each other ─── */
int is_even(int n);
int is_odd(int n);
int is_even(int n) { return n == 0 ? 1 : is_odd(n - 1);  }
int is_odd (int n) { return n == 0 ? 0 : is_even(n - 1); }

int main(void) {
    printf("5! = %lld
", factorial(5));     /* 120 */
    printf("fib(10) naive = %lld
", fib_naive(10));  /* 55 - ok for n<=35 */
    printf("fib(10) iter  = %lld
", fib_iter(10));   /* 55 */

    int sorted[] = {2, 5, 8, 12, 16, 23, 38, 56, 72};
    int n = (int)(sizeof(sorted)/sizeof(sorted[0]));
    printf("bsearch 23 = index %d
", bsearch_rec(sorted, 0, n-1, 23)); /* 5 */

    printf("is_even(4)=%d  is_odd(7)=%d
", is_even(4), is_odd(7)); /* 1 1 */
    return 0;
}

Common Mistakes

  • No base case - `int f(int n) { return n * f(n-1); }` never stops. Every recursive function needs at least one base case that does NOT recurse.
  • Stack overflow from deep recursion - `factorial(1000000)` → stack overflow on most systems. The default stack is 1-8 MB. Use iteration for large `n`, or increase stack size with `ulimit -s` on Linux.
  • Naive Fibonacci in production - `fib_naive(45)` takes seconds. Use `fib_iter` (O(n) time) or memoization. Never use tree recursion without caching.

Tip

Tip

Practice Recursion Call Stack Depth Tail Calls in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Each recursive call pushes a new stack frame until the base case initiates return unwinding

Practice Task

Note

Practice Task - (1) Write a working example of Recursion Call Stack Depth Tail Calls from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Recursion Call Stack Depth Tail Calls is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • Recursion is a function calling itself.
  • No base case - `int f(int n) { return n * f(n-1); }` never stops. Every recursive function needs at least one base case that does NOT recurse.
  • Stack overflow from deep recursion - `factorial(1000000)` → stack overflow on most systems. The default stack is 1-8 MB. Use iteration for large `n`, or increase stack size with `ulimit -s` on Linux.
  • Naive Fibonacci in production - `fib_naive(45)` takes seconds. Use `fib_iter` (O(n) time) or memoization. Never use tree recursion without caching.

Topics in This Module

0/8 done