Base Case and Recursive Case in C

Intermediate
6 min read
Updated: Nov 2026
1 example

The Two Parts of Every Recursion

Every correct recursion has at least one base case (answered directly) and a recursive case that moves toward it. Work done before the recursive call happens on the way down; work done after happens as the calls unwind. Forgetting the base case, or not shrinking the input, produces infinite recursion and a stack overflow.

Remember
void f(int n) {
    if (n == 0) { /* base case */ return; }
    /* work on the way down */
    f(n - 1);
    /* work on the way up   */
}

Cheat Sheet

Keep these facts handy while you work with Base Case and Recursive Case in C in C.

TopicDetail
Base caseStops the recursion
Recursive caseCalls with smaller input
Before the callRuns descending
After the callRuns unwinding

C Examples

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

Example 1 — Down and up

Print on the way down and again on the way up.

C
#include <stdio.h>

static void f(int n) {
    if (n == 0) {
        printf("base case reached\n");
        return;
    }
    printf("down %d\n", n);
    f(n - 1);
    printf("up %d\n", n);
}

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

How It Works

  1. Calls for 3, 2, and 1 print "down" before recursing.
  2. The base case ends the descent.
  3. The pending calls then resume and print "up" in reverse order.

Edge Cases & Pitfalls

Check these before relying on Base Case and Recursive Case in C in real programs.

inf

Input never shrinks

f(n) calling f(n) repeats forever.

wrong

Wrong base value

Starting at a negative number never hits n == 0. Use n <= 0.

Key Takeaways

  • Base case ends the chain.
  • Before the call runs down.
  • After the call runs up.

One line: Every correct recursion has at least one base case (answered directly) and a recursive case that moves toward it.

Frequently Asked Questions

The condition under which the function returns directly without calling itself.
The function calls itself indefinitely until the stack overflows.
Continue with "C Factorial and Fibonacci" next. Compute factorial recursively and see why naive Fibonacci is slow. The C Recursion and Backtracking hub lists every topic in order.

Next: Factorial and Fibonacci

Compute factorial recursively and see why naive Fibonacci is slow.

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