codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › C recursion

C recursion

A function that calls itself.

Needs a base case to stop. Each call uses stack memory, so very deep recursion can crash.

Example code

#include <stdio.h>
int fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }
int main() {
  printf("%d\n", fact(5));
  return 0;
}

Run this code in the online editor

← C math functionsC function pointers →