C Recursion

Beginner
8 min read
Updated: Nov 2026
2 examples

What Is Recursion?

Recursion is when a function calls itself on a smaller version of the problem. Every recursive function needs a base case that stops the calls and a recursive case that moves toward it. Each call gets its own stack frame, so very deep recursion can overflow the stack.

Remember
int factorial(int n) {
    if (n <= 1) return 1;            /* base case      */
    return n * factorial(n - 1);     /* recursive case */
}

Cheat Sheet

Keep these facts handy while you work with Recursion in C.

TopicDetail
Base caseStops the recursion
Recursive caseReduces the problem
CostOne stack frame per call
AlternativeA loop, when simpler

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Arrays.

Example 1 — Factorial

n! = n * (n - 1)! with 0! = 1! = 1.

C
#include <stdio.h>

long factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main(void) {
    printf("0! = %ld\n", factorial(0));
    printf("5! = %ld\n", factorial(5));
    return 0;
}

How It Works

  1. factorial(5) becomes 5 * factorial(4) and so on down to factorial(1).
  2. The base case returns 1, then each frame multiplies on the way back.
  3. 5 * 4 * 3 * 2 * 1 = 120.

Example 2 — Countdown that prints on the way down

The printf runs before the recursive call.

C
#include <stdio.h>

void countdown(int n) {
    if (n == 0) {
        printf("Liftoff!\n");
        return;
    }
    printf("%d\n", n);
    countdown(n - 1);
}

int main(void) {
    countdown(3);
    return 0;
}

How It Works

  1. Each call prints n and calls itself with n - 1.
  2. When n reaches 0 the base case prints and returns.
  3. Swap the order of the print and the call to print on the way back instead.

Edge Cases & Pitfalls

Check these before relying on Recursion in real programs.

base

Missing base case

Without a base case the recursion never ends and crashes with a stack overflow.

deep

Very deep recursion

Calling a function 100,000 times deep may exhaust the stack. Use a loop for simple counting.

Key Takeaways

  • Always: a base case first.
  • Progress: each call must get closer to it.
  • Frames: each call uses stack memory.

One line: Recursion is when a function calls itself on a smaller version of the problem.

Frequently Asked Questions

Recursion is a function calling itself to solve a smaller instance of the same problem until a base case is reached.
Usually a bit, because of call overhead and stack use. It can be much clearer for trees and divide-and-conquer problems.
Continue with "C Arrays" next. Store many values of one type: one-dimensional, two-dimensional, multidimensional, and variable length arrays. The C Functions hub lists every topic in order.

Next: Arrays

Store many values of one type: one-dimensional, two-dimensional, multidimensional, and variable length arrays.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful