C Recursive Factorial: Base Case

C Recursive Factorial Program

A classroom recursion exercise needs both a smaller subproblem and a valid result when no smaller work remains.

Each call multiplies n by factorial(n−1), with one as the zero-input base result.

Before this exercise, review functions and copied parameters and recursion and stopping cases. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why recursive factorial: base case uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Call the function with zero and explain the base result.

Recursive Factorial: Base Case: follow the data

  1. Given data

    A classroom recursion exercise needs both a smaller subproblem and a valid result when no smaller work remains.

  2. Rule

    Each call multiplies n by factorial(n−1), with one as the zero-input base result.

  3. Observed result

    Factorial: 24

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

For n=4, the products return as 1,1,2,6 and24; a zero base value would destroy every later product.

Each call owns a separate parameter value and waits for its smaller call to return. The result combines while calls finish, not merely while the arguments decrease.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
static unsigned long long factorial(unsigned n) {
  if (n == 0) return 1;
  return n * factorial(n - 1);
}
int main(void) {
  printf("Factorial: %llu\n", factorial(4));
  return 0;
}

Expected output

Factorial: 24
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
static unsigned long long factorial(unsigned n) {
  if (n == 0) return 1;
  return n * factorial(n - 1);
}
int main(void) {
  printf("Factorial: %llu\n", factorial(4));
  return 0;
}

The starting argument is 4.

For n=4, the products return as 1,1,2,6 and24; a zero base value would destroy every later product.

Follow the worked calculation

  1. Start with the sample

    The starting argument is 4.

  2. Apply the rule

    For n=4, the products return as 1,1,2,6 and24; a zero base value would destroy every later product.

  3. Check the result

    Factorial: 24

    Factorial: 24
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
static unsigned long long factorial(unsigned n) {
  if (n == 0) return 1;
  return n * factorial(n - 1);
}
int main(void) {
  printf("Factorial: %llu\n", factorial(4));
  return 0;
}

Call the function with zero and explain the base result.

Worked answer

The base call returns 1 without making another recursive call. That result is the appropriate arithmetic identity for factorial, not an arbitrary stopping label.

C17 · complete program

#include <stdio.h>
static unsigned long long factorial(unsigned n) {
  if (n == 0) return 1;
  return n * factorial(n - 1);
}
int main(void) {
  printf("Factorial: %llu\n", factorial(0));
  return 0;
}

Expected output

Factorial: 1
Run in C compiler →

Recognize the important boundaries

The demonstrated nonnegative input is at most 20, fitting the minimum unsigned-long-long range. Larger factorials need checked arithmetic or a larger-number representation.

Recursion depth consumes execution resources. A loop can express the same small arithmetic recurrence without building a deep call chain.

Check your understanding

Choose the answer, then compare the reasoning for every option.

What must a terminating recursive call do?
Choose one answer. You can try again.

Answer and explanation

Reach a base case without another recursive call.

Correct. The recursive step must move allowed inputs toward that case.

Use the idea independently

Each call multiplies n by factorial(n−1), with one as the zero-input base result.

Continue with call and return tracing.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Calculate Power Using Recursion →